Skip to content
Open
Show file tree
Hide file tree
Changes from 11 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
105 changes: 88 additions & 17 deletions ls8/cpu.c
Original file line number Diff line number Diff line change
@@ -1,29 +1,50 @@
#include "cpu.h"

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define DATA_LEN 6

unsigned char 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 value)
{
cpu->ram[address] = value;
}

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

FILE *fp;
char line[1024];

if ((fp = fopen(file, "r")) == NULL)
{
fprintf(stderr, "File not found.\n");
exit(1);
}

int address = 0;

for (int i = 0; i < DATA_LEN; i++) {
cpu->ram[address++] = data[i];
}
fp = fopen(file, "r");
while(fgets(line, sizeof(line), fp) != NULL){
char *endptr;
unsigned char value;
value = strtoul(line, &endptr, 2);

// TODO: Replace this with something less hard-coded
if(endptr == line){
// printf("Ignoring this line.\n");
continue;
}

// printf("'line' - %s\n'value' - %d\n'address' - %d", line, value, address);
cpu->ram[address++] = value;
}
}

/**
Expand All @@ -46,13 +67,59 @@ 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

// printf("in run");
while (running) {
// TODO
// 1. Get the value of the current instruction (in address PC).
unsigned char ir = cpu_ram_read(cpu, cpu->PC);
// 2. Figure out how many operands this next instruction requires
// 3. Get the appropriate value(s) of the operands following this instruction
unsigned char operandA = cpu_ram_read(cpu, cpu->PC + 1);
unsigned char operandB = cpu_ram_read(cpu, cpu->PC + 2);
// 4. switch() over it to decide on a course of action.
switch (ir)
{
case HLT:
{
running = 0;
break;
}
case LDI:
{
cpu->registers[operandA] = operandB;
cpu->PC += 3;
break;
}
case MUL:
{
cpu->registers[operandA] *= cpu->registers[operandB];
cpu->PC += 3;
break;
}
case POP:
{
cpu->registers[operandA] = cpu_ram_read(cpu, cpu->registers[7]);
cpu->ram[cpu->registers[7]++] = 0x00;
cpu->PC += 2;
break;
}
case PRN:
{
printf("%d\n", cpu->registers[operandA]);
cpu->PC += 2;
break;
}
case PUSH:
{
cpu_ram_write(cpu, --cpu->registers[7], cpu->registers[operandA]);
cpu->PC += 2;
break;
}
default:
{
break;
}
}
// 5. Do whatever the instruction should do according to the spec.
// 6. Move the PC to the next instruction.
}
Expand All @@ -63,5 +130,9 @@ void cpu_run(struct cpu *cpu)
*/
void cpu_init(struct cpu *cpu)
{
// TODO: Initialize the PC and other special registers
cpu->PC = 0;
cpu->FL = 0;
memset(cpu->ram, 0, 8 * sizeof(unsigned char));
memset(cpu->registers, 0, 256 * sizeof(unsigned char));
cpu->registers[7] = 0xF4;
}
9 changes: 8 additions & 1 deletion ls8/cpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@
struct cpu {
// TODO
// PC
unsigned char PC;
unsigned char FL;
// registers (array)
unsigned char registers[8];
// ram (array)
unsigned char ram[256];
};

// ALU operations
Expand All @@ -23,11 +27,14 @@ enum alu_op {
#define LDI 0b10000010
#define HLT 0b00000001
#define PRN 0b01000111
#define MUL 0b10100010
#define POP 0b01000110
#define PUSH 0b01000101
// TODO: more instructions here. These can be used in cpu_run().

// Function declarations

extern void cpu_load(struct cpu *cpu);
extern void cpu_load(struct cpu *cpu, char *file);
extern void cpu_init(struct cpu *cpu);
extern void cpu_run(struct cpu *cpu);

Expand Down
10 changes: 8 additions & 2 deletions ls8/ls8.c
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
#include <stdio.h>
#include <stdlib.h>
#include "cpu.h"

/**
* Main
*/
int main(void)
int main(int argc, char *argv[])
{
if (argc < 2)
{
fprintf(stderr, "A file must be given.\n");
exit(1);
}
struct cpu cpu;

cpu_init(&cpu);
cpu_load(&cpu);
cpu_load(&cpu, argv[1]);
cpu_run(&cpu);

return 0;
Expand Down