Skip to content
Open
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
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
84 changes: 70 additions & 14 deletions ls8/cpu.c
Original file line number Diff line number Diff line change
@@ -1,29 +1,40 @@
#include "cpu.h"
#include <string.h>
#include <stdio.h>

#define DATA_LEN 6

/**
* 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
};

// TODO: Replace this with something less hard-coded
FILE *file_pointer;
char line[1024];
int address = 0;

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

if(value == HLT) {
cpu->ram[address++] = value;
break; /* only break case is when HLT instruction is received */
}

cpu->ram[address++] = value;
}
}

// TODO: Replace this with something less hard-coded
unsigned char cpu_ram_read(struct cpu *cpu, unsigned char address) {
return cpu->ram[address]; /* reads the cpu struct's contents at this specific location in memory */
}

void cpu_ram_write(struct cpu *cpu, unsigned char address, unsigned char value) {
cpu->ram[address] = value; /* set new data in memory */
}

/**
Expand All @@ -50,11 +61,51 @@ void cpu_run(struct cpu *cpu)
while (running) {
// TODO
// 1. Get the value of the current instruction (in address PC).
unsigned char IR = cpu_ram_read(cpu, cpu->PC); /* IR = Instruction Register */

// 2. Figure out how many operands this next instruction requires
unsigned char op0 = cpu_ram_read(cpu, cpu->PC + 1);
unsigned char op1 = cpu_ram_read(cpu, cpu->PC + 2);
// 3. Get the appropriate value(s) of the operands following this instruction

// 4. switch() over it to decide on a course of action.
/* case pseudocode for instruction handlers:
LDI -- set cpu->registers first value to next value
HLT -- set running to 0 and break
PRN -- print current register value (same as first value in LDI)
*/
// 5. Do whatever the instruction should do according to the spec.
switch(IR) {

case LDI:
{
cpu->registers[op0] = op1;
cpu->PC += 3;
break;
}

case PRN:
{
printf("%d\n", cpu->registers[op0]);
cpu->PC += 2;
break;
}

case HLT:
{
running = 0;
break;
}

default:
{
printf("Command 0x%02x\n not recognized.", IR);
running = 0;
break;
}
}
// 6. Move the PC to the next instruction.
/* removed previous code and added this step to cases */
}
}

Expand All @@ -64,4 +115,9 @@ void cpu_run(struct cpu *cpu)
void cpu_init(struct cpu *cpu)
{
// TODO: Initialize the PC and other special registers
/* need to initialize PC, registers, and ram (all elements of CPU struct) */

cpu->PC = 0;
memset(cpu->registers, 0, 8);
memset(cpu->ram, 0, 256);
}
8 changes: 7 additions & 1 deletion ls8/cpu.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
#ifndef _CPU_H_
#define _CPU_H_

/* initial commit */

// Holds all information about the CPU
struct cpu {
// TODO
// PC
unsigned char PC;
// registers (array)
unsigned char registers[8]; /* covers R0-R7 */
// ram (array)
unsigned char *ram[256];
unsigned char FL; /* flag for later use */
};

// ALU operations
Expand All @@ -27,7 +33,7 @@ enum alu_op {

// 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
4 changes: 2 additions & 2 deletions ls8/ls8.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
/**
* Main
*/
int main(void)
int main(int argc, char *argv[])
{
struct cpu cpu;

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

return 0;
Expand Down