Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions ls8/cpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,18 @@
/**
* Load the binary bytes from a .ls8 source file into a RAM array
*/

unsigned char cpu_ram_read(struct cpu *cpu, unsigned char mem)
{
return cpu->ram[mem];
}

void cpu_ram_write(struct cpu *cpu, unsigned char mem, unsigned char mdr)
{
cpu->ram[mem] = mdr;
}


void cpu_load(struct cpu *cpu)
{
const int DATA_LEN = 6;
Expand Down Expand Up @@ -45,11 +57,30 @@ void alu(struct cpu *cpu, enum alu_op op, unsigned char regA, unsigned char regB
void cpu_run(struct cpu *cpu)
{
int running = 1; // True until we get a HLT instruction
unsigned char IR, operandA, operandB;

while (running) {
// TODO
// 1. Get the value of the current instruction (in address PC).
IR = cpu_ram_read(cpu, cpu->PC);
operandA = cpu_ram_read(cpu, cpu->PC+1);
operandB = cpu_ram_read(cpu, cpu->PC+1);
// 2. switch() over it to decide on a course of action.
switch (IR) {
case LDI:
cpu->registers[operandA] = operandB;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Looks good!

cpu->PC+=3;
break;
case PRN:
printf("%d\n", cpu->registers[operandA]);
cpu->PC+=2;
break;
case HTL:
running = 0;
cpu->PC+=1;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Eventually you may come across a way to increase the PC using the instruction code itself (hint: this involves bit shifting).

break
}

// 3. Do whatever the instruction should do according to the spec.
// 4. Move the PC to the next instruction.
}
Expand All @@ -61,6 +92,11 @@ void cpu_run(struct cpu *cpu)
void cpu_init(struct cpu *cpu)
{
// TODO: Initialize the PC and other special registers
cpu->PC = 0;



// TODO: Zero registers and RAM
memset(cpu->ram, 0, sizeof cpu->ram);
memset(cpu->registers, 0, sizeof cpu->registers);
}
5 changes: 5 additions & 0 deletions ls8/cpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
struct cpu {
// TODO
// PC
unsigned char PC;
// registers (array)
unsigned char registers[6];
// ram (array)
unsigned char ram[256];
};

// Instructions
Expand All @@ -15,6 +18,8 @@ struct cpu {
// literals should be used.

#define LDI 0b10000010
#define PRN 0b01000111
#define HTL 0b00000001
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I think this should be HLT for "halt". But as long as you have it the same in both places, it'll still work.

// TODO: more instructions here. These can be used in cpu_run().

// Function declarations
Expand Down