Skip to content
Open
9 changes: 6 additions & 3 deletions ls8/cpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,16 @@ void cpu_run(struct cpu *cpu)
case CALL:
sp--;
stack[sp] = cpu->registers[operandA];
cpu->PC = operandA;
stack[operandA];
cpu->PC = cpu->registers[operandA];

cpu_ram_read(cpu, cpu->PC);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be writing to RAM instead:
cpu_ram_write(cpu, stack[sp], cpu->pc+2);
But the way you setup your stack will conlfict with this. It may be easier for you to just create push and pop functions to increment and derement the stack pointer and then write the value you want to push to ram at the stack pointer.
Something like this:
push (struct *cpu, unsigned char val): { cpu->registers[sp] --; cpu_ram_write(cpu, val, cpu->reg[sp]); }

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So with what you're doing I think you're just reading from RAM at the program count, and never even referencing the stack.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not entirely sure how you can call the right instruction this way.

break;

case RET:
cpu->PC = stack[sp];
cpu->registers[operandA] = stack[sp];
sp++;

cpu->PC = cpu->registers[operandA];
break;


Expand Down