minor fixes & error checking but still JE
Q bug?
This commit is contained in:
@@ -153,10 +153,8 @@ void assemble(char* filename)
|
||||
}
|
||||
else if (strncmp(line, "JEQ", 3) == 0)
|
||||
{
|
||||
sscanf(line, "%s %[^,], %d", instruction, reg1, &addr);
|
||||
int reg1_n = reg1[1] - '0';
|
||||
sscanf(line, "%s %d", instruction, &addr);
|
||||
buffer[i++] = JEQ;
|
||||
buffer[i++] = reg1_n;
|
||||
buffer[i++] = addr;
|
||||
}
|
||||
else if (strncmp(line, "JMP", 3) == 0)
|
||||
|
||||
21
cpu.c
21
cpu.c
@@ -26,6 +26,7 @@ void cpu_init()
|
||||
cpu.halted = false;
|
||||
cpu.equal_flag = false;
|
||||
cpu.flag_clear_delay = 0;
|
||||
cpu.increment = 0;
|
||||
for (size_t i=0; i<NUM_REGISTERS; i++)
|
||||
{
|
||||
cpu.reg[i] = 0;
|
||||
@@ -132,6 +133,7 @@ void cpu_exec(uint8_t opcode)
|
||||
cpu.halted = true;
|
||||
break;
|
||||
}
|
||||
cpu.increment++;
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -185,6 +187,17 @@ void cpu_run()
|
||||
{
|
||||
uint8_t opcode = cpu.memory[cpu.pc++];
|
||||
cpu_exec(opcode);
|
||||
if (cpu.pc >= MEM_SIZE)
|
||||
{
|
||||
printf("\nProgram ended - program counter reached max memsize\n");
|
||||
break;
|
||||
}
|
||||
|
||||
if (cpu.increment > INF_LOOP_THRESHOLD)
|
||||
{
|
||||
printf("\nProgram ended - reached infinite loop threshold\n");
|
||||
break;
|
||||
}
|
||||
}
|
||||
printf("\n[END CPU OUTPUT]\n");
|
||||
}
|
||||
@@ -210,12 +223,12 @@ void cpu_dump()
|
||||
|
||||
void mem_dump()
|
||||
{
|
||||
printf("\n*** Memory dump ***\n");
|
||||
printf("\n*** Memory dump ***");
|
||||
for (size_t i=0; i<MEM_SIZE; i++)
|
||||
{
|
||||
if (i%20 == 0)
|
||||
{
|
||||
puts("");
|
||||
printf("\n%04ld: ", i);
|
||||
}
|
||||
|
||||
switch (cpu.memory[i])
|
||||
@@ -254,10 +267,14 @@ void mem_dump()
|
||||
break;
|
||||
|
||||
// General purpose registers (colored foreground)
|
||||
//case 0x00:
|
||||
case 0x01:
|
||||
case 0x02:
|
||||
case 0x03:
|
||||
case 0x04:
|
||||
case 0x05:
|
||||
case 0x06:
|
||||
case 0x07:
|
||||
printf("\e[36m%02x\e[0m ", cpu.memory[i]);
|
||||
break;
|
||||
default:
|
||||
|
||||
2
cpu.h
2
cpu.h
@@ -4,6 +4,7 @@
|
||||
#define MEM_SIZE 256
|
||||
#define NUM_REGISTERS 8
|
||||
#define BUF_MAX 256
|
||||
#define INF_LOOP_THRESHOLD 10000
|
||||
|
||||
/*
|
||||
* Instruction set
|
||||
@@ -55,6 +56,7 @@ typedef struct
|
||||
bool halted;
|
||||
bool equal_flag;
|
||||
int flag_clear_delay;
|
||||
int increment;
|
||||
} CPU_t;
|
||||
|
||||
#endif
|
||||
|
||||
4
makefile
4
makefile
@@ -7,5 +7,9 @@ all: asm
|
||||
asm:
|
||||
$(CC) assembler/*.c $(CFLAGS) -o das
|
||||
|
||||
run:
|
||||
./das program.asm
|
||||
./dumb8 program.bin
|
||||
|
||||
clean:
|
||||
rm dumb8 das
|
||||
|
||||
10
program.asm
10
program.asm
@@ -1,6 +1,10 @@
|
||||
;this is a comment
|
||||
PUT R0, 5
|
||||
PUT R3, 2
|
||||
ADD R0, R3
|
||||
|
||||
PUT R0, 2
|
||||
PUT R1, 2
|
||||
CMP R0, R1
|
||||
JEQ 12
|
||||
HLT
|
||||
|
||||
PUT R7, 10
|
||||
HLT
|
||||
|
||||
Reference in New Issue
Block a user