feat: we be drawing IBM logos in the terminal like it is the 1970s
This commit is contained in:
+16
-1
@@ -40,4 +40,19 @@
|
||||
- [x] Parse individual instructions from binary CHIP-8 file
|
||||
- [x] Print all instructions from CHIP-8 file
|
||||
- [x] Write all CHIP-8 IBM logo opcodes to CSV file
|
||||
- [ ] Implement opcodes present in the IMB logo CHIP-8 file
|
||||
- [x] Implement opcodes present in the IMB logo CHIP-8 file
|
||||
|
||||
| x | Opcode | Type | Description |
|
||||
|---|--------|------|------------------------------|
|
||||
| x | `00E0` | 00E0 | Clear display |
|
||||
| x | `1nnn` | 1nnn | Jump to address |
|
||||
| x | `3xkk` | 3xkk | Skip next if Vx == kk |
|
||||
| x | `6xkk` | 6xkk | Load register with immediate |
|
||||
| x | `7xkk` | 7xkk | Add immediate to Vx |
|
||||
| x | `ANNN` | ANNN | Set index register |
|
||||
| x | `Bnnn` | Bnnn | Jump to V0 + addr |
|
||||
| x | `Dxyn` | Dxyn | Draw sprite |
|
||||
|
||||
- [ ] Migrate drawing to raylib window
|
||||
- [ ] Implement remaining instructions
|
||||
- [ ] Handle keyboard input
|
||||
|
||||
@@ -5,11 +5,14 @@
|
||||
#include <sys/time.h>
|
||||
#include <windows.h>
|
||||
#include <signal.h>
|
||||
#include <string.h>
|
||||
|
||||
#define MEMORY_SIZE 4096
|
||||
#define PROGRAM_START 512
|
||||
#define CPU_CLOCK_HZ 600
|
||||
#define TIMER_HZ 60
|
||||
#define SCREEN_WIDTH 64
|
||||
#define SCREEN_HEIGHT 32
|
||||
|
||||
const int CYCLES_PER_FRAME = CPU_CLOCK_HZ / TIMER_HZ;
|
||||
|
||||
@@ -32,8 +35,11 @@ enum ExitCodes {
|
||||
typedef struct ChipEight {
|
||||
uint8_t memory[MEMORY_SIZE];
|
||||
uint16_t pc;
|
||||
uint16_t I;
|
||||
uint8_t V[16];
|
||||
uint8_t dt;
|
||||
uint8_t st;
|
||||
uint32_t display[SCREEN_WIDTH * SCREEN_HEIGHT];
|
||||
} chip_eight_t;
|
||||
|
||||
void println(const char *format, ...) {
|
||||
@@ -59,6 +65,25 @@ long get_time() {
|
||||
return ms;
|
||||
}
|
||||
|
||||
void draw(chip_eight_t *chip) {
|
||||
printf("\033[J\033[H\033[?25l");
|
||||
|
||||
for (int i = 0; i < SCREEN_HEIGHT; i++) {
|
||||
for (int j = 0; j < SCREEN_WIDTH; j++) {
|
||||
uint32_t screen_pixel = chip->display[i * SCREEN_WIDTH + j];
|
||||
|
||||
if (screen_pixel == 1) {
|
||||
printf("█");
|
||||
} else {
|
||||
printf(" ");
|
||||
}
|
||||
}
|
||||
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
// Sets up Ctrl+C handling
|
||||
signal(SIGINT, handle_sigint);
|
||||
@@ -108,21 +133,12 @@ int main(int argc, char *argv[]) {
|
||||
chip->pc = PROGRAM_START;
|
||||
chip->dt = 0;
|
||||
chip->st = 0;
|
||||
memset(chip->display, 0, sizeof(chip->display));
|
||||
|
||||
println("File '%s' loaded successfully", file_path);
|
||||
|
||||
fclose(file_ptr);
|
||||
|
||||
FILE *csv_file_ptr = fopen("./imb_logo_opcodes.csv", "a");
|
||||
|
||||
if (csv_file_ptr == NULL) {
|
||||
println("Failed to open csv file for writing");
|
||||
return -1;
|
||||
}
|
||||
|
||||
char *csv_headers = "opcode,nnn,n,x,y,kk\n";
|
||||
fprintf(csv_file_ptr, csv_headers);
|
||||
|
||||
// main loop
|
||||
while(keep_running) {
|
||||
if (chip->pc > MEMORY_SIZE) {
|
||||
@@ -164,14 +180,14 @@ int main(int argc, char *argv[]) {
|
||||
// oper: >> 8
|
||||
// oper: &
|
||||
// mask: 00001111 00000000 (3840)
|
||||
uint16_t x = (opcode >> 8) & 3840;
|
||||
uint16_t x = (opcode & 3840) >> 8;
|
||||
|
||||
// y - A 4-bit value, the upper 4 bits of the low byte of the instruction
|
||||
// i.e. 00000000 11110000
|
||||
// oper: >> 4
|
||||
// oper: &
|
||||
// mask: 00000000 11110000 (240)
|
||||
uint16_t y = (opcode >> 4) & 240;
|
||||
uint16_t y = (opcode & 240) >> 4;
|
||||
|
||||
// kk or byte - An 8-bit value, the lowest 8 bits of the instruction
|
||||
// i.e. 00000000 11111111
|
||||
@@ -179,33 +195,96 @@ int main(int argc, char *argv[]) {
|
||||
// mask: 00000000 11111111 (255)
|
||||
uint16_t kk = opcode & 255;
|
||||
|
||||
// write headers of variable name to .csv file
|
||||
// create a string for each opcode that is comma separated
|
||||
// then write that string into a .csv file
|
||||
|
||||
char *csv_format = "%x,%d,%d,%d,%d,%d\n";
|
||||
int len_of_str = snprintf(NULL, 0, csv_format, opcode, nnn, n, x, y, kk);
|
||||
char *csv_str = malloc((len_of_str + 1) * sizeof(char));
|
||||
// mask: 11110000 00000000
|
||||
switch(opcode & 61440) {
|
||||
// Clear display (00E0)
|
||||
case 0:
|
||||
if (opcode == 224) {
|
||||
// println("Clean the display");
|
||||
memset(chip->display, 0, sizeof(chip->display));
|
||||
draw(chip);
|
||||
}
|
||||
break;
|
||||
case 4096:
|
||||
// println("Jump to address: %d", nnn);
|
||||
chip->pc = nnn;
|
||||
break;
|
||||
case 12288:
|
||||
// println("Skip next");
|
||||
|
||||
if (csv_str == NULL) {
|
||||
println("Failed to allocate memory for csv string");
|
||||
return -1;
|
||||
if (chip->V[x] == kk) {
|
||||
// println("Skipping");
|
||||
chip->pc += 2;
|
||||
} else {
|
||||
// println("Not skipping");
|
||||
}
|
||||
|
||||
break;
|
||||
case 24576:
|
||||
// println("Load register with immedate");
|
||||
chip->V[x] = kk;
|
||||
break;
|
||||
case 28672:
|
||||
// println("Add immedate");
|
||||
chip->V[x] += kk;
|
||||
break;
|
||||
case 40960:
|
||||
// println("Set index register");
|
||||
chip->I = nnn;
|
||||
break;
|
||||
case 45056:
|
||||
// println("Jump to V0 + addr");
|
||||
chip->pc = nnn + chip->V[0];
|
||||
break;
|
||||
case 53248:
|
||||
// println("Draw sprite");
|
||||
uint8_t x_coordinate = chip->V[x] % SCREEN_WIDTH;
|
||||
uint8_t y_coordinate = chip->V[y] % SCREEN_HEIGHT;
|
||||
chip->V[15] = 0;
|
||||
|
||||
for (int row = 0; row < n; row++) {
|
||||
uint8_t sprite_byte = chip->memory[chip->I + row];
|
||||
|
||||
if ((y_coordinate + row) >= SCREEN_HEIGHT) {
|
||||
break;
|
||||
}
|
||||
|
||||
for (int col = 0; col < 8; col++) {
|
||||
if ((x_coordinate + col) >= SCREEN_WIDTH) {
|
||||
break;
|
||||
}
|
||||
|
||||
// 0 0 0 0 0 0 0 0
|
||||
// 1 0 0 0 0 0 0 0
|
||||
// 0 1 0 0 0 0 0 0
|
||||
// 0 0 1 0 0 0 0 0
|
||||
uint8_t sprite_pixel = sprite_byte & (128 >> col);
|
||||
|
||||
// ****
|
||||
// **x*
|
||||
int sprite_screen_index = ((y_coordinate + row) * SCREEN_WIDTH) + (x_coordinate + col);
|
||||
uint32_t *screen_pixel = &chip->display[sprite_screen_index];
|
||||
|
||||
if (sprite_pixel != 0) {
|
||||
if (*screen_pixel == 1) {
|
||||
chip->V[15] = 1;
|
||||
}
|
||||
|
||||
*screen_pixel ^= 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
draw(chip);
|
||||
break;
|
||||
default:
|
||||
// println("Unhandled opcode: %x", opcode);
|
||||
break;
|
||||
}
|
||||
|
||||
snprintf(csv_str, len_of_str + 1, csv_format, opcode, nnn, n, x, y, kk);
|
||||
fprintf(csv_file_ptr, csv_str);
|
||||
|
||||
free(csv_str);
|
||||
csv_str = NULL;
|
||||
|
||||
println("opcode: %d", opcode);
|
||||
println("nnn : %d", nnn);
|
||||
println("n : %d", n);
|
||||
println("x : %d", x);
|
||||
println("y : %d", y);
|
||||
println("kk : %d", kk);
|
||||
}
|
||||
|
||||
|
||||
if (chip->dt > 0) {
|
||||
chip->dt--;
|
||||
}
|
||||
@@ -226,8 +305,6 @@ int main(int argc, char *argv[]) {
|
||||
}
|
||||
}
|
||||
|
||||
fclose(csv_file_ptr);
|
||||
|
||||
free(chip);
|
||||
|
||||
return COMPLETED_SUCCESSFULLY;
|
||||
|
||||
Reference in New Issue
Block a user