Skip to content
Open
Show file tree
Hide file tree
Changes from 5 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
70 changes: 54 additions & 16 deletions ls8/cpu.c
Original file line number Diff line number Diff line change
@@ -1,29 +1,38 @@
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "cpu.h"

#define DATA_LEN 6

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

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

/**
* Load the binary bytes from a .ls8 source file into a RAM array
*/
void cpu_load(struct cpu *cpu)
{
char data[DATA_LEN] = {
// From print8.ls8
0b10000010, // LDI R0,8
0b00000000,
0b00001000,
0b01000111, // PRN R0
0b00000000,
0b00000001 // HLT
};

void cpu_load(struct cpu *cpu, char *path)
{
FILE *filepointer = fopen(path, "r");
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Awesome now you'll be able to handle any set of instructions.

if(filepointer == NULL){
printf("Couldn't open the program you entered: %s\n", path);
exit(2);
}
int address = 0;

for (int i = 0; i < DATA_LEN; i++) {
cpu->ram[address++] = data[i];
char line[8000];
while(fgets(line, sizeof(line), filepointer) != NULL){
char *endpointer;
if(endpointer == line){
continue;
}

}

// TODO: Replace this with something less hard-coded
}

/**
Expand All @@ -50,11 +59,32 @@ void cpu_run(struct cpu *cpu)
while (running) {
// TODO
// 1. Get the value of the current instruction (in address PC).
unsigned char instruction = cpu_ram_read(cpu, cpu->PC);
// 2. Figure out how many operands this next instruction requires
// Not needed if we use a switch statement
// 3. Get the appropriate value(s) of the operands following this instruction
unsigned char op0 = cpu_ram_read(cpu, cpu->PC + 1);
unsigned char op1 = cpu_ram_read(cpu, cpu->PC + 2);
printf("trace: %02X: %02X %02X %02X\n", cpu->PC, instruction, op0, op1);
// 4. switch() over it to decide on a course of action.
// 5. Do whatever the instruction should do according to the spec.
// 6. Move the PC to the next instruction.
switch (instruction){
case LDI:
cpu->regs[op0] = op1;
cpu->PC += 3;
break;
case PRN:
printf("%d\n", cpu->regs[op0]);
cpu->PC += 2;
break;
case HLT: //HLT the cpu and exit the emulator
running = 0;
break;
default:
printf("unexpected instruction 0x%02X at 0x%02X\n", instruction, cpu->PC);
exit(1);
}
}
}

Expand All @@ -64,4 +94,12 @@ void cpu_run(struct cpu *cpu)
void cpu_init(struct cpu *cpu)
{
// TODO: Initialize the PC and other special registers
for(int i = 0; i < 7; i++){
cpu->regs[i] = 0;
}
cpu->regs[7] = 0xF4;

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

// ALU operations
Expand Down
2 changes: 1 addition & 1 deletion ls8/ls8.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ int main(void)
cpu_run(&cpu);

return 0;
}
}