perfaware/part1: Get listing 0039 working
This commit is contained in:
parent
2a17186f74
commit
d7b6598b1c
252
part1/sim8086.c
252
part1/sim8086.c
@ -25,6 +25,11 @@ struct S86_Globals {
|
||||
bool write_to_console;
|
||||
};
|
||||
|
||||
typedef struct S86_BufferIterator {
|
||||
S86_Buffer buffer;
|
||||
size_t index;
|
||||
} S86_BufferIterator;
|
||||
|
||||
S86_Globals s86_globals;
|
||||
|
||||
#define S86_STRINGIFY2(token) #token
|
||||
@ -41,6 +46,11 @@ S86_Globals s86_globals;
|
||||
#define S86_CAST(Type) (Type)
|
||||
|
||||
bool S86_BufferIsValid(S86_Buffer buffer);
|
||||
S86_BufferIterator S86_BufferIteratorInit(S86_Buffer buffer);
|
||||
bool S86_BufferIteratorHasMoreBytes(S86_BufferIterator it);
|
||||
uint8_t S86_BufferIteratorPeekByte(S86_BufferIterator it);
|
||||
uint8_t S86_BufferIteratorNextByte(S86_BufferIterator *it);
|
||||
|
||||
S86_Buffer S86_FileRead(char const *file_path);
|
||||
void S86_FileFree(S86_Buffer buffer);
|
||||
void S86_PrintLn(S86_Str8 string);
|
||||
@ -52,6 +62,37 @@ bool S86_BufferIsValid(S86_Buffer buffer)
|
||||
return result;
|
||||
}
|
||||
|
||||
S86_BufferIterator S86_BufferIteratorInit(S86_Buffer buffer)
|
||||
{
|
||||
S86_BufferIterator result = {0};
|
||||
result.buffer = buffer;
|
||||
return result;
|
||||
}
|
||||
|
||||
bool S86_BufferIteratorHasMoreBytes(S86_BufferIterator it)
|
||||
{
|
||||
bool result = S86_BufferIsValid(it.buffer) && it.index < it.buffer.size;
|
||||
return result;
|
||||
}
|
||||
|
||||
uint8_t S86_BufferIteratorPeekByte(S86_BufferIterator it)
|
||||
{
|
||||
S86_ASSERT(S86_BufferIsValid(it.buffer));
|
||||
S86_ASSERT(it.index < it.buffer.size);
|
||||
uint8_t result = it.buffer.data[it.index];
|
||||
return result;
|
||||
}
|
||||
|
||||
uint8_t S86_BufferIteratorNextByte(S86_BufferIterator *it)
|
||||
{
|
||||
S86_ASSERT(it);
|
||||
S86_ASSERT(S86_BufferIsValid(it->buffer));
|
||||
S86_ASSERT(it->index < it->buffer.size);
|
||||
uint8_t result = it->buffer.data[it->index++];
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
S86_Buffer S86_FileRead(char const *file_path)
|
||||
{
|
||||
S86_Buffer result = {0};
|
||||
@ -230,49 +271,6 @@ struct S86_Instruction {
|
||||
.op_bits1 = 0b0000'0000},
|
||||
};
|
||||
|
||||
typedef enum S86_OpDataSize S86_OpDataSize;
|
||||
enum S86_OpDataSize { S86_OpDataSize_Byte, S86_OpDataSize_Word };
|
||||
|
||||
typedef struct S86_InstructionStream {
|
||||
uint8_t bytes[6];
|
||||
uint8_t size;
|
||||
} S86_InstructionStream;
|
||||
|
||||
typedef struct S86_BufferIterator {
|
||||
S86_Buffer buffer;
|
||||
size_t index;
|
||||
} S86_BufferIterator;
|
||||
|
||||
S86_BufferIterator S86_BufferIteratorInit(S86_Buffer buffer)
|
||||
{
|
||||
S86_BufferIterator result = {0};
|
||||
result.buffer = buffer;
|
||||
return result;
|
||||
}
|
||||
|
||||
bool S86_BufferIteratorHasMoreBytes(S86_BufferIterator it)
|
||||
{
|
||||
bool result = S86_BufferIsValid(it.buffer) && it.index < it.buffer.size;
|
||||
return result;
|
||||
}
|
||||
|
||||
uint8_t S86_BufferIteratorPeekByte(S86_BufferIterator it)
|
||||
{
|
||||
S86_ASSERT(S86_BufferIsValid(it.buffer));
|
||||
S86_ASSERT(it.index < it.buffer.size);
|
||||
uint8_t result = it.buffer.data[it.index];
|
||||
return result;
|
||||
}
|
||||
|
||||
uint8_t S86_BufferIteratorNextByte(S86_BufferIterator *it)
|
||||
{
|
||||
S86_ASSERT(it);
|
||||
S86_ASSERT(S86_BufferIsValid(it->buffer));
|
||||
S86_ASSERT(it->index < it->buffer.size);
|
||||
uint8_t result = it->buffer.data[it->index++];
|
||||
return result;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
if (argc != 2) {
|
||||
@ -316,8 +314,9 @@ int main(int argc, char **argv)
|
||||
S86_BufferIterator buffer_it = S86_BufferIteratorInit(buffer);
|
||||
while (S86_BufferIteratorHasMoreBytes(buffer_it)) {
|
||||
|
||||
S86_InstructionStream stream = {0};
|
||||
stream.bytes[stream.size++] = S86_BufferIteratorNextByte(&buffer_it);
|
||||
char op_code_bytes[2] = {0};
|
||||
size_t op_code_size = 0;
|
||||
op_code_bytes[op_code_size++] = S86_BufferIteratorNextByte(&buffer_it);
|
||||
|
||||
// NOTE: Match the assembly bytes to the desired instruction
|
||||
// =====================================================================
|
||||
@ -331,7 +330,7 @@ int main(int argc, char **argv)
|
||||
|
||||
// NOTE: Check first instruction byte
|
||||
// =================================================================
|
||||
if ((stream.bytes[0] & item->op_mask0) != item->op_bits0)
|
||||
if ((op_code_bytes[0] & item->op_mask0) != item->op_bits0)
|
||||
continue;
|
||||
|
||||
// NOTE Check multi-byte instruction
|
||||
@ -341,8 +340,8 @@ int main(int argc, char **argv)
|
||||
bool instruction_matched = true;
|
||||
if (item->op_mask1) {
|
||||
// TODO: This assumes the iterator is valid
|
||||
stream.bytes[stream.size++] = S86_BufferIteratorNextByte(&buffer_it);
|
||||
instruction_matched = (stream.bytes[stream.size - 1] & item->op_mask1) == item->op_bits1;
|
||||
op_code_bytes[op_code_size++] = S86_BufferIteratorNextByte(&buffer_it);
|
||||
instruction_matched = (op_code_bytes[op_code_size - 1] & item->op_mask1) == item->op_bits1;
|
||||
}
|
||||
|
||||
if (instruction_matched) {
|
||||
@ -353,111 +352,126 @@ int main(int argc, char **argv)
|
||||
|
||||
// NOTE: Disassemble bytes to assembly mnemonics
|
||||
// =================================================================
|
||||
S86_ASSERT(op_code_size > 0 && op_code_size <= S86_ARRAY_UCOUNT(op_code_bytes));
|
||||
S86_ASSERT(instruction_type != S86_InstructionType_Count && "Unknown instruction");
|
||||
switch (instruction_type) {
|
||||
case S86_InstructionType_MOVRegOrMemToOrFromReg: {
|
||||
// NOTE: Instruction does not have opcode bits in the 2nd byte
|
||||
S86_ASSERT(stream.size == 1);
|
||||
stream.bytes[stream.size++] = S86_BufferIteratorNextByte(&buffer_it);
|
||||
S86_ASSERT(op_code_size == 1);
|
||||
op_code_bytes[op_code_size++] = S86_BufferIteratorNextByte(&buffer_it);
|
||||
|
||||
uint8_t d = (stream.bytes[0] & 0b0000'0010) >> 1;
|
||||
uint8_t w = (stream.bytes[0] & 0b0000'0001) >> 0;
|
||||
uint8_t mod = (stream.bytes[1] & 0b1100'0000) >> 6;
|
||||
uint8_t reg = (stream.bytes[1] & 0b0011'1000) >> 3;
|
||||
uint8_t rm = (stream.bytes[1] & 0b0000'0111) >> 0;
|
||||
uint8_t d = (op_code_bytes[0] & 0b0000'0010) >> 1;
|
||||
uint8_t w = (op_code_bytes[0] & 0b0000'0001) >> 0;
|
||||
uint8_t mod = (op_code_bytes[1] & 0b1100'0000) >> 6;
|
||||
uint8_t reg = (op_code_bytes[1] & 0b0011'1000) >> 3;
|
||||
uint8_t rm = (op_code_bytes[1] & 0b0000'0111) >> 0;
|
||||
S86_ASSERT(d < 2);
|
||||
S86_ASSERT(w < 2);
|
||||
S86_ASSERT(mod < 4);
|
||||
S86_ASSERT(reg < 8);
|
||||
S86_ASSERT(rm < 8);
|
||||
|
||||
uint8_t instr_dest = d ? reg : rm;
|
||||
uint8_t instr_src = d ? rm : reg;
|
||||
if (mod == 0b11) {
|
||||
// NOTE: Register-to-register move
|
||||
// =========================================================
|
||||
S86_Str8 src_op = REGISTER_FIELD_ENCODING[w][d ? rm : reg];
|
||||
S86_Str8 dest_op = REGISTER_FIELD_ENCODING[w][d ? reg : rm];
|
||||
S86_PrintLnFmt("mov %.*s, %.*s", S86_STR8_FMT(dest_op), S86_STR8_FMT(src_op));
|
||||
} else {
|
||||
// NOTE: Memory mode w/ effective address calculation
|
||||
// =========================================================
|
||||
uint16_t displacement = 0;
|
||||
if (mod == 0b01) { // Mem mode 8 bit displacement
|
||||
displacement = S86_BufferIteratorNextByte(&buffer_it);
|
||||
} else if (mod == 0b10 || (mod == 0b00 && rm == 0b110)) { // Mem mode 16 bit displacement
|
||||
uint8_t disp_lo = S86_BufferIteratorNextByte(&buffer_it);
|
||||
uint8_t disp_hi = S86_BufferIteratorNextByte(&buffer_it);
|
||||
displacement = (uint16_t)disp_lo << 0 | (uint16_t)disp_hi << 8;
|
||||
} else {
|
||||
S86_ASSERT(mod == 0b00 /*Mem mode (no displacement)*/);
|
||||
if (mod == 0b00)
|
||||
S86_ASSERT(rm != 0b110 && "16 bit displacement should be handled in (mod == 0b10) branch");
|
||||
}
|
||||
|
||||
// S86_OpDataSize data_size = w ? S86_OpDataSize_Word : S86_OpDataSize_Byte;
|
||||
S86_ASSERT(mod == 0b11); // register-to-register
|
||||
// NOTE: Generate the effective address calculation string
|
||||
// =========================================================
|
||||
char effective_addr_buffer[64] = {0};
|
||||
int effective_addr_size = 0;
|
||||
|
||||
S86_Str8 src_register = REGISTER_FIELD_ENCODING[w][instr_src];
|
||||
S86_Str8 dest_register = REGISTER_FIELD_ENCODING[w][instr_dest];
|
||||
effective_addr_buffer[effective_addr_size++] = '[';
|
||||
if (rm == 0b110) {
|
||||
effective_addr_buffer[effective_addr_size++] = 'b';
|
||||
effective_addr_buffer[effective_addr_size++] = 'p';
|
||||
if (displacement) {
|
||||
effective_addr_size += snprintf(effective_addr_buffer + effective_addr_size,
|
||||
sizeof(effective_addr_buffer) - effective_addr_size,
|
||||
" + %u",
|
||||
displacement);
|
||||
}
|
||||
} else {
|
||||
S86_Str8 base_calc = {0};
|
||||
switch (rm) {
|
||||
case 0b000: base_calc = S86_STR8("bx + si"); break;
|
||||
case 0b001: base_calc = S86_STR8("bx + di"); break;
|
||||
case 0b010: base_calc = S86_STR8("bp + si"); break;
|
||||
case 0b011: base_calc = S86_STR8("bp + di"); break;
|
||||
case 0b100: base_calc = S86_STR8("si"); break;
|
||||
case 0b101: base_calc = S86_STR8("di"); break;
|
||||
case 0b111: base_calc = S86_STR8("bx"); break;
|
||||
default: S86_ASSERT(!"Invalid rm value, must be 3 bits"); break;
|
||||
}
|
||||
|
||||
S86_PrintLnFmt("mov %.*s, %.*s", S86_STR8_FMT(dest_register), S86_STR8_FMT(src_register));
|
||||
memcpy(effective_addr_buffer + effective_addr_size, base_calc.data, base_calc.size);
|
||||
effective_addr_size += S86_CAST(int)base_calc.size;
|
||||
|
||||
#if 0
|
||||
if (mod == 0b01) {
|
||||
// 8 bit displacement
|
||||
} else if (mode == 0b10) {
|
||||
// 16 bit displacement
|
||||
if (mod == 0b01 || mod == 0b10) {
|
||||
effective_addr_size += snprintf(effective_addr_buffer + effective_addr_size,
|
||||
sizeof(effective_addr_buffer) - effective_addr_size,
|
||||
" + %u",
|
||||
displacement);
|
||||
}
|
||||
}
|
||||
effective_addr_buffer[effective_addr_size++] = ']';
|
||||
|
||||
// NOTE: Disassemble
|
||||
// =========================================================
|
||||
S86_Str8 effective_addr = { .data = effective_addr_buffer, .size = effective_addr_size };
|
||||
S86_Str8 dest_op = d ? REGISTER_FIELD_ENCODING[w][reg] : effective_addr;
|
||||
S86_Str8 src_op = d ? effective_addr : REGISTER_FIELD_ENCODING[w][reg];
|
||||
S86_PrintLnFmt("mov %.*s, %.*s", S86_STR8_FMT(dest_op), S86_STR8_FMT(src_op));
|
||||
}
|
||||
#endif
|
||||
|
||||
} break;
|
||||
|
||||
case S86_InstructionType_MOVImmediateToRegOrMem: {
|
||||
S86_ASSERT(stream.size == 2);
|
||||
uint8_t w = (stream.bytes[0] & 0b0000'0001) >> 0;
|
||||
uint8_t mod = (stream.bytes[1] & 0b1100'0000) >> 6;
|
||||
uint8_t rm = (stream.bytes[1] & 0b0000'0111) >> 0;
|
||||
|
||||
uint16_t displacement = 0;
|
||||
if (mod == 0b01) {
|
||||
// 8 bit displacement
|
||||
stream.bytes[stream.size++] = S86_BufferIteratorNextByte(&buffer_it);
|
||||
displacement = stream.bytes[stream.size - 1];
|
||||
} else if (mod == 0b10) {
|
||||
// 16 bit displacement
|
||||
stream.bytes[stream.size++] = S86_BufferIteratorNextByte(&buffer_it);
|
||||
stream.bytes[stream.size++] = S86_BufferIteratorNextByte(&buffer_it);
|
||||
displacement = (uint16_t)stream.bytes[2] << 0 | (uint16_t)stream.bytes[3] << 8;
|
||||
} else {
|
||||
// NOTE: We can't have register-to-register (mod == 0b11)
|
||||
// as this instruction is an immediate to register/memory
|
||||
S86_ASSERT(mod == 0b00);
|
||||
}
|
||||
|
||||
stream.bytes[stream.size++] = S86_BufferIteratorNextByte(&buffer_it);
|
||||
uint16_t data = stream.bytes[stream.size - 1];
|
||||
|
||||
if (w) { // 16 bit data
|
||||
stream.bytes[stream.size++] = S86_BufferIteratorNextByte(&buffer_it);
|
||||
data |= (uint16_t)(stream.bytes[stream.size - 1]) << 8;
|
||||
}
|
||||
|
||||
S86_Str8 effective_address_registers;
|
||||
switch (rm) {
|
||||
case 0b000: effective_address_registers = S86_STR8("bx + si"); break;
|
||||
case 0b001: effective_address_registers = S86_STR8("bx + di"); break;
|
||||
case 0b010: effective_address_registers = S86_STR8("bp + si"); break;
|
||||
case 0b011: effective_address_registers = S86_STR8("bp + di"); break;
|
||||
case 0b100: effective_address_registers = S86_STR8("si"); break;
|
||||
case 0b101: effective_address_registers = S86_STR8("di"); break;
|
||||
case 0b110: effective_address_registers = S86_STR8("??"); S86_ASSERT(!"Unhandled instruction"); break;
|
||||
case 0b111: effective_address_registers = S86_STR8("bx"); break;
|
||||
default: S86_ASSERT(!"Invalid rm value, must be 3 bits"); break;
|
||||
}
|
||||
|
||||
S86_ASSERT(op_code_size == 2);
|
||||
#if 0
|
||||
uint8_t w = (op_code_bytes[0] & 0b0000'0001) >> 0;
|
||||
uint8_t mod = (op_code_bytes[1] & 0b1100'0000) >> 6;
|
||||
uint8_t rm = (op_code_bytes[1] & 0b0000'0111) >> 0;
|
||||
#endif
|
||||
S86_ASSERT(!"Unhandled instruction");
|
||||
} break;
|
||||
|
||||
case S86_InstructionType_MOVImmediateToReg: {
|
||||
// NOTE: Parse opcode control bits
|
||||
// =============================================================
|
||||
S86_ASSERT(stream.size == 1);
|
||||
uint8_t w = (stream.bytes[0] & 0b0000'1000) >> 3;
|
||||
uint8_t reg = (stream.bytes[0] & 0b0000'0111) >> 0;
|
||||
S86_ASSERT(op_code_size == 1);
|
||||
uint8_t w = (op_code_bytes[0] & 0b0000'1000) >> 3;
|
||||
uint8_t reg = (op_code_bytes[0] & 0b0000'0111) >> 0;
|
||||
|
||||
// NOTE: Parse data payload
|
||||
// =============================================================
|
||||
stream.bytes[stream.size++] = S86_BufferIteratorNextByte(&buffer_it);
|
||||
int16_t data = stream.bytes[stream.size - 1];
|
||||
|
||||
uint16_t data = S86_BufferIteratorNextByte(&buffer_it);
|
||||
if (w) { // 16 bit data
|
||||
stream.bytes[stream.size++] = S86_BufferIteratorNextByte(&buffer_it);
|
||||
data |= (int16_t)(stream.bytes[stream.size - 1]) << 8;
|
||||
uint8_t data_hi = S86_BufferIteratorNextByte(&buffer_it);
|
||||
data |= (uint16_t)(data_hi) << 8;
|
||||
}
|
||||
|
||||
// NOTE: Disassemble
|
||||
// =============================================================
|
||||
S86_Str8 dest_register = REGISTER_FIELD_ENCODING[w][reg];
|
||||
S86_PrintLnFmt("mov %.*s, %d", S86_STR8_FMT(dest_register), data);
|
||||
S86_PrintLnFmt("mov %.*s, %d", S86_STR8_FMT(dest_register), (int16_t)data);
|
||||
} break;
|
||||
|
||||
case S86_InstructionType_MOVMemToAccum: {
|
||||
|
BIN
project.rdbg
BIN
project.rdbg
Binary file not shown.
Loading…
Reference in New Issue
Block a user