Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions asm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,5 @@ node asm source.asm
## TODO

* Port this to Python

<!-- comment -->
57 changes: 56 additions & 1 deletion ls8/cpu.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#include "cpu.h"
#include <stdio.h>
#include <string.h>

#define DATA_LEN 6

#define LR 7
/**
* Load the binary bytes from a .ls8 source file into a RAM array
*/
Expand Down Expand Up @@ -50,18 +52,71 @@ 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);
// 2. Figure out how many operands this next instruction requires

unsigned char operandA = 0;
unsigned char operandB = 0;
unsigned int numOp = IR >> 6;

// 3. Get the appropriate value(s) of the operands following this instruction

if (numOp == 2)
{
unsigned char operandA = cpu_ram_read(cpu, (cpu->pc + 1) & 0xff);
unsigned char operandB = cpu_ram_read(cpu, (cpu->pc + 2) & 0xff);
}

// 4. switch() over it to decide on a course of action.

switch (IR)
{
case LDI:
printf("%d\n", cpu->reg[operandB]);
cpu->reg[operandA] = operandB;
cpu->pc += 2;
printf("%d\n", cpu->reg[operandB]);
break;
case PRN:
cpu->pc += 2;
printf("%d\n", cpu->reg[operandA]);
break;
case HLT:
running = 0;
break;
default:
printf("Error");
break;
// 5. Do whatever the instruction should do according to the spec.
// 6. Move the PC to the next instruction.
}
}

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

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

/**
* Initialize a CPU struct
*/
void cpu_init(struct cpu *cpu)
{
// TODO: Initialize the PC and other special registers
for (int i = 0; i <= 6; i++)
{
cpu->reg[i] = 0;
}

cpu->reg[7] = 0;
cpu->pc = 0;

// set everything in ram to 0
memset(cpu->ram, 0, sizeof cpu->ram);
}
12 changes: 9 additions & 3 deletions ls8/cpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
// Holds all information about the CPU
struct cpu {
// TODO
unsigned int PC;
// PC
// registers (array)
unsigned char reg[8];
// ram (array)
unsigned char ram[256];
};

// ALU operations
Expand All @@ -20,9 +23,12 @@ enum alu_op {
// These use binary literals. If these aren't available with your compiler, hex
// literals should be used.

#define LDI 0b10000010
#define HLT 0b00000001
#define PRN 0b01000111
#define LDI 0b10000010
#define HLT 0b00000001
#define PRN 0b01000111

// Stack instructions

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

// Function declarations
Expand Down
9 changes: 7 additions & 2 deletions ls8/ls8.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@
/**
* Main
*/
int main(void)
int main(int argc, char **argv)
{
struct cpu cpu;
if (argc != 2)
{
fprintf(stderr, "incorrect input\n");
return 1;
}

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

return 0;
Expand Down