Skip to content

Commit

Permalink
x86emu big endian fix (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
BSzili authored Jun 18, 2024
1 parent 32e9e62 commit 6d7ce3c
Show file tree
Hide file tree
Showing 9 changed files with 114 additions and 97 deletions.
4 changes: 2 additions & 2 deletions drivers/pentprim/fti8_piz.c
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ void TRAPEZIUM_ZI_I8_D16(int32_t *half_count, uint32_t *half_i, uint32_t *half_d
// adc_&dirn edx,0 ; carry into integer part of z
ADC_D(edx.v, 0, dirn);
// mov bl,[edi+ebp*2] ; fetch old z
ebx.short_val[0] = ((uint16_t *)work.depth.base)[edi.v / 2 + ebp.int_val];
ebx.short_low = ((uint16_t *)work.depth.base)[edi.v / 2 + ebp.int_val];

// add_&dirn eax,ecx ; step i
ADD_SET_CF_D(eax.v, ecx.v, dirn);
Expand All @@ -142,7 +142,7 @@ void TRAPEZIUM_ZI_I8_D16(int32_t *half_count, uint32_t *half_i, uint32_t *half_d
}

// mov [edi+ebp*2],dx ; store pixel and depth (prefix cannot be avoided since
((uint16_t *)work.depth.base)[edi.v / 2 + ebp.int_val] = edx.short_val[0];
((uint16_t *)work.depth.base)[edi.v / 2 + ebp.int_val] = edx.short_low;
// mov [esi+ebp],al ; two byte writes would fill the write buffers)
((uint8_t *)work.colour.base)[esi.v + ebp.v] = eax.l;

Expand Down
12 changes: 6 additions & 6 deletions drivers/pentprim/fti8pizp.c
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ static inline void ScanlineRender_ZPT_I8_D16(int size, int dirn, int udirn, int
// ;

// mov dl,[ebp]
edx.short_val[0] = *ebp.ptr_16;
edx.short_low = *ebp.ptr_16;
// mov cl,[eax+esi]
ecx.l = esi.ptr_8[eax.v];

Expand All @@ -172,7 +172,7 @@ static inline void ScanlineRender_ZPT_I8_D16(int size, int dirn, int udirn, int

// cmp bx,dx
// ja nodraw
if (ebx.short_val[0] > edx.short_val[0]) {
if (ebx.short_low > edx.short_low) {
goto nodraw;
}

Expand Down Expand Up @@ -215,7 +215,7 @@ static inline void ScanlineRender_ZPT_I8_D16(int size, int dirn, int udirn, int
// ; Store texel and z
// ;
// mov [ebp],bx
*ebp.ptr_16 = ebx.short_val[0];
*ebp.ptr_16 = ebx.short_low;
// mov [edi],cl
*edi.ptr_8 = ecx.l;
}
Expand Down Expand Up @@ -630,7 +630,7 @@ static inline void ScanlineRender_ZPTI_I8_D16(int size, int dirn, int udirn, int
// ;

// mov dx,[ebp]
edx.short_val[0] = *ebp.ptr_16;
edx.short_low = *ebp.ptr_16;
// xor ecx,ecx
ecx.v = 0;
// mov ebx,work.tsl.z
Expand All @@ -640,7 +640,7 @@ static inline void ScanlineRender_ZPTI_I8_D16(int size, int dirn, int udirn, int

// cmp bx,dx
// ja nodraw
if (ebx.short_val[0] > edx.short_val[0]) {
if (ebx.short_low > edx.short_low) {
goto nodraw;
}

Expand Down Expand Up @@ -679,7 +679,7 @@ static inline void ScanlineRender_ZPTI_I8_D16(int size, int dirn, int udirn, int
// ; Look texel up in shade table, store texel and z
// ;
// mov [ebp],bx
*ebp.ptr_16 = ebx.short_val[0];
*ebp.ptr_16 = ebx.short_low;
// mov cl,[ecx+edx]
ecx.l = edx.ptr_8[ecx.v];
// mov [edi],cl
Expand Down
4 changes: 2 additions & 2 deletions drivers/pentprim/zb8.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,15 @@ void DRAW_Z_I8_D16(uint32_t *minorX, uint32_t *d_minorX, char direction, int32_t
// mov dl,[ebp+2*ecx]
// ;The following line needs some more experimentation to prove its usefullness in real application
// mov dh,[ebp+2*ecx+1]
edx.short_val[0] = ((uint16_t *)work.depth.base)[ebp.v / 2 + ecx.int_val];
edx.short_low = ((uint16_t *)work.depth.base)[ebp.v / 2 + ecx.int_val];
// cmp eax,edx
// ja noPlot
if (eax.v > edx.v) {
goto noPlot;
}
// ; writes
// mov [ebp+2*ecx],ax
((uint16_t *)work.depth.base)[ebp.v / 2 + ecx.int_val] = eax.short_val[0];
((uint16_t *)work.depth.base)[ebp.v / 2 + ecx.int_val] = eax.short_low;
// mov [edi+ecx],bl
((uint8_t *)work.colour.base)[edi.v + ecx.v] = ebx.l;
noPlot:
Expand Down
16 changes: 8 additions & 8 deletions drivers/pentprim/zb8awtm.c
Original file line number Diff line number Diff line change
Expand Up @@ -384,13 +384,13 @@ static inline void DRAW_ZT_I8(uint32_t *minorX, uint32_t *d_minorX, char directi

drawPixel:
// mov bx,[ebp+2*ecx]
ebx.short_val[0] = ((uint16_t *)work.depth.base)[ebp.v / 2 + ecx.int_val];
ebx.short_low = ((uint16_t *)work.depth.base)[ebp.v / 2 + ecx.int_val];
// mov dx,word ptr workspace.c_z+2
edx.short_val[0] = ((uint16_t *)&workspace.c_z)[1];
edx.short_low = ((uint16_t *)&workspace.c_z)[1];

// cmp dx,bx
// ja noPlot
if(edx.short_val[0] > ebx.short_val[0]) {
if(edx.short_low > ebx.short_low) {
goto noPlot;
}

Expand Down Expand Up @@ -439,7 +439,7 @@ static inline void DRAW_ZT_I8(uint32_t *minorX, uint32_t *d_minorX, char directi
// else
} else {
// mov [ebp+2*ecx],dx
((uint16_t *)work.depth.base)[ebp.v / 2 + ecx.int_val] = edx.short_val[0];
((uint16_t *)work.depth.base)[ebp.v / 2 + ecx.int_val] = edx.short_low;
// mov [edi+ecx],bl
((uint8_t *)work.colour.base)[edi.v + ecx.v] = ebx.l;
// endif
Expand Down Expand Up @@ -618,13 +618,13 @@ static inline void DRAW_ZTI_I8(uint32_t *minorX, uint32_t *d_minorX, char direct

drawPixel:
// mov bx,[ebp+2*ecx]
ebx.short_val[0] = ((uint16_t *)work.depth.base)[ebp.v / 2 + ecx.int_val];
ebx.short_low = ((uint16_t *)work.depth.base)[ebp.v / 2 + ecx.int_val];
// mov dx,word ptr workspace.c_z+2
edx.short_val[0] = ((uint16_t *)&workspace.c_z)[1];
edx.short_low = ((uint16_t *)&workspace.c_z)[1];

// cmp dx,bx
// ja noPlot
if(edx.short_val[0] > ebx.short_val[0]) {
if(edx.short_low > ebx.short_low) {
goto noPlot;
}

Expand Down Expand Up @@ -684,7 +684,7 @@ static inline void DRAW_ZTI_I8(uint32_t *minorX, uint32_t *d_minorX, char direct
// mov bl,byte ptr [eax+ebx]
ebx.l = ((uint8_t *)work.shade_table)[ebx.v];
// mov [ebp+2*ecx],dx
((uint16_t *)work.depth.base)[ebp.v / 2 + ecx.int_val] = edx.short_val[0];
((uint16_t *)work.depth.base)[ebp.v / 2 + ecx.int_val] = edx.short_low;
// mov [edi+ecx],bl
((uint8_t *)work.colour.base)[edi.v + ecx.v] = ebx.l;
// endif
Expand Down
4 changes: 2 additions & 2 deletions drivers/pentprim/zb8p2lit.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ void DRAW_ZTI_I8_D16_POW2(uint32_t *minorX, uint32_t *d_minorX, char direction,
// mov ah,byte ptr[workspace.c_i+2]
eax.h = BYTE2(workspace.c_i);
// ja noPlot
if (ebx.short_val[0] > edx.short_val[0]) {
if (ebx.short_low > edx.short_low) {
goto noPlot;
}
// test al,al
Expand All @@ -105,7 +105,7 @@ void DRAW_ZTI_I8_D16_POW2(uint32_t *minorX, uint32_t *d_minorX, char direction,
goto noPlot;
}
// mov [ebp+2*ecx],bx ;two cycles
((uint16_t *)work.depth.base)[ebp.v / 2 + ecx.int_val] = ebx.short_val[0];
((uint16_t *)work.depth.base)[ebp.v / 2 + ecx.int_val] = ebx.short_low;
// mov [esi+ecx],al
((uint8_t *)work.colour.base)[esi.v + ecx.v] = eax.l;

Expand Down
4 changes: 2 additions & 2 deletions drivers/pentprim/zb8p2ulb.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ void DRAW_ZTB_I8_D16_POW2(uint32_t *minorX, uint32_t *d_minorX, char direction,

// cmp dx,bx ;two cycles
// ja noPlot
if(edx.short_val[0] > ebx.short_val[0]) {
if(edx.short_low > ebx.short_low) {
goto noPlot;
}
// mov al,[esi+eax]
Expand All @@ -109,7 +109,7 @@ void DRAW_ZTB_I8_D16_POW2(uint32_t *minorX, uint32_t *d_minorX, char direction,

// mov [ebp+2*ecx],dl
// mov [ebp+2*ecx+1],dh
((uint16_t *)work.depth.base)[ebp.v / 2 + ecx.int_val] = edx.short_val[0];
((uint16_t *)work.depth.base)[ebp.v / 2 + ecx.int_val] = edx.short_low;

// and eax,0ffffh
eax.v &= 0xffff;
Expand Down
4 changes: 2 additions & 2 deletions drivers/pentprim/zb8p2unl.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ void DRAW_ZT_I8_D16_POW2(uint32_t *minorX, uint32_t *d_minorX, char direction, i

// cmp dx,bx ;two cycles
// ja noPlot
if(edx.short_val[0] > ebx.short_val[0]) {
if(edx.short_low > ebx.short_low) {
goto noPlot;
}

Expand All @@ -114,7 +114,7 @@ void DRAW_ZT_I8_D16_POW2(uint32_t *minorX, uint32_t *d_minorX, char direction, i

// mov [ebp+2*ecx],dl
// mov [ebp+2*ecx+1],dh
((uint16_t *)work.depth.base)[ebp.v / 2 + ecx.int_val] = edx.short_val[0];
((uint16_t *)work.depth.base)[ebp.v / 2 + ecx.int_val] = edx.short_low;
// mov [edi+ecx],al
((uint8_t *)work.colour.base)[edi.v + ecx.v] = eax.l;

Expand Down
119 changes: 68 additions & 51 deletions x86emu/include/x86emu.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@
#include <stdint.h>
#include <math.h>

enum X86_OP {
X86_OP_REG,
X86_OP_MEM32,
X86_OP_PTR,
X86_OP_IMM
};

enum {
X87_OP_FLOAT,
X87_OP_DOUBLE,
X87_OP_ST,
X87_OP_MEM32,
X87_OP_MEM64
};
// enum X86_OP {
// X86_OP_REG,
// X86_OP_MEM32,
// X86_OP_PTR,
// X86_OP_IMM
// };

// enum {
// X87_OP_FLOAT,
// X87_OP_DOUBLE,
// X87_OP_ST,
// X87_OP_MEM32,
// X87_OP_MEM64
// };

typedef struct x86_reg {
union {
Expand All @@ -26,12 +26,29 @@ typedef struct x86_reg {
float float_val;
//unsigned char bytes[8];
struct {
#if BR_ENDIAN_BIG
uint16_t pad1;
// "high" byte
uint8_t h;
// "low" byte
uint8_t l;
#else
// "low" byte
uint8_t l;
// "high" byte
uint8_t h;
#endif
};
//uint16_t short_val[2];
struct {
#if BR_ENDIAN_BIG
uint16_t short_high;
uint16_t short_low;
#else
uint16_t short_low;
uint16_t short_high;
#endif
};
uint16_t short_val[2];
// these are a bit of a hack to allow 64bit pointers on occasion
void *ptr_v;
uint8_t *ptr_8;
Expand All @@ -40,43 +57,43 @@ typedef struct x86_reg {

} x86_reg;

typedef union x86_mem {
// typedef union x86_mem {
// uint32_t uint_val;
// float float_val;
void *ptr_val;
// void *ptr_val;
// unsigned char bytes[8];

} x86_mem;

typedef struct x86_operand {
union {
x86_reg *reg;
x86_mem mem;
void *ptr;
uint32_t imm;
};
char type;
} x86_operand;

typedef struct x87_operand {
struct { // union?
float float_val;
double double_val;
int st_index;
void *mem;
unsigned char bytes[8];
};
char type;
} x87_operand;

x87_operand x87_op_f(float f);
x87_operand x87_op_i(int i);
x87_operand x87_op_mem32(void *ptr);
x87_operand x87_op_mem64(void *ptr);
x86_operand x86_op_reg(x86_reg *r);
x86_operand x86_op_mem32(void *bytes);
x86_operand x86_op_ptr(void *ptr);
x86_operand x86_op_imm(uint32_t imm);
// } x86_mem;

// typedef struct x86_operand {
// union {
// x86_reg *reg;
// x86_mem mem;
// void *ptr;
// uint32_t imm;
// };
// char type;
// } x86_operand;

// typedef struct x87_operand {
// struct { // union?
// float float_val;
// double double_val;
// int st_index;
// void *mem;
// unsigned char bytes[8];
// };
// char type;
// } x87_operand;

// x87_operand x87_op_f(float f);
// x87_operand x87_op_i(int i);
// x87_operand x87_op_mem32(void *ptr);
// x87_operand x87_op_mem64(void *ptr);
// x86_operand x86_op_reg(x86_reg *r);
// x86_operand x86_op_mem32(void *bytes);
// x86_operand x86_op_ptr(void *ptr);
// x86_operand x86_op_imm(uint32_t imm);

// extern x86_reg *eax, *ebx, *ecx, *edx, *esi, *ebp, *edi;
// void x86emu_init();
Expand Down Expand Up @@ -271,9 +288,9 @@ extern x86_reg eax, ebx, ecx, edx, ebp, edi, esi;
val1 = val1 - (val2 + x86_state.cf);

#define ROR16(dest) \
x86_state.x86_swap = dest.short_val[0]; \
dest.short_val[0] = dest.short_val[1]; \
dest.short_val[1] = x86_state.x86_swap;
x86_state.x86_swap = dest.short_low; \
dest.short_low = dest.short_high; \
dest.short_high = x86_state.x86_swap;


#endif
Loading

0 comments on commit 6d7ce3c

Please sign in to comment.