commit a0157a07599bb6887ec778b6cc21c8eef7430770 Author: Joseph Ferano Date: Fri Jan 12 22:20:51 2024 +0700 Part01-01: Project setup and basic decoding diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5d9b464 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/decode diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..283abf6 --- /dev/null +++ b/Makefile @@ -0,0 +1,15 @@ +CFLAGS=-g -std=c2x -fsanitize=address -fno-omit-frame-pointer -Wall -Wextra -pedantic -O0 +CC=gcc + +.PHONY: build clean run all + +all: decode + +decode: decode.c + $(CC) $(CFLAGS) $< -o $@ + +run: all + ./decode + +clean: + rm -vf *.so *.o decode diff --git a/README.org b/README.org new file mode 100644 index 0000000..89d04ee --- /dev/null +++ b/README.org @@ -0,0 +1,8 @@ +#+OPTIONS: toc:nil + +* 8086 CPU Emulator +Following along with [[https://www.computerenhance.com/p/table-of-contents][Performance Aware by Computer Enhance]] +* License +This project is licensed under the terms of the MIT license. For more +information, see the included LICENSE file. However, some of this is related to +a paid course so maybe pay for the course if you want to use these files? diff --git a/asm_files/01-01-37.asm b/asm_files/01-01-37.asm new file mode 100644 index 0000000..d7361c1 --- /dev/null +++ b/asm_files/01-01-37.asm @@ -0,0 +1,7 @@ +; ======================================================================== +; LISTING 37 +; ======================================================================== + +bits 16 + +mov bx, cx ; mov 001, 011 diff --git a/decode.c b/decode.c new file mode 100644 index 0000000..c25cfb1 --- /dev/null +++ b/decode.c @@ -0,0 +1,98 @@ +#include +#include + +#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0])) + +enum Instruction +{ + INST_MOV = 0b10001000 +}; + +enum Register +{ + REG_AL_AX = 0b000, + REG_CL_CX = 0b001, + REG_DL_DX = 0b010, + REG_BL_BX = 0b011, + REG_AH_SP = 0b100, + REG_CH_BP = 0b101, + REG_DH_SI = 0b110, + REG_BH_DI = 0b111, +}; + +#define REG_AX "ax" +#define REG_CX "cx" +#define REG_BX "bx" + +enum Mode +{ + MODE_MEM_NOD = 0b00, + MODE_MEM_08D = 0b01, + MODE_MEM_16D = 0b10, + MODE_REGMODE = 0b11, +}; + +int main(int argc, char **argv) +{ + if (argc < 2) + { + printf("Usage: Please provide assembled instructions as input\n"); + exit(0); + } + unsigned char buf[256]; + FILE *f = fopen(argv[1], "r"); + if (!f) + { + perror("fopen\n"); + return EXIT_FAILURE; + } + size_t bytes_read = fread(buf, sizeof(*buf), ARRAY_SIZE(buf), f); + + printf("; Decoded 8086 Assembly Instructions\n\n"); + printf("bits 16\n\n"); + for (int i = 0; i < (int)bytes_read / 2; i += 2) + { + char byte = buf[i]; + switch (byte & 0b11111100) + { + case INST_MOV: + char high_byte = buf[i+1]; + // char w = byte & 0b00000001; + // char d = byte & 0b00000010; + char mod = (high_byte & 0b11000000) >> 6; + char reg = (high_byte & 0b00111000) >> 3; + char r_m = (high_byte & 0b00000111); + printf("mov "); + if (mod == MODE_REGMODE) + { + char *dest_reg_name = NULL; + switch (r_m) + { + case REG_CL_CX: + dest_reg_name = REG_CX; + break; + case REG_BL_BX: + dest_reg_name = REG_BX; + break; + } + printf("%s, ", dest_reg_name); + + char *src_reg_name = NULL; + switch (reg) + { + case REG_CL_CX: + src_reg_name = REG_CX; + break; + case REG_BL_BX: + src_reg_name = REG_BX; + break; + } + printf("%s", src_reg_name); + } + break; + default: + perror("Unrecognized Instruction\n"); + } + } + printf("\n"); +}