From 9bc1d6efec4851ea4551f118a609caf279584604 Mon Sep 17 00:00:00 2001 From: Barbara Hunter Date: Mon, 4 Mar 2019 17:52:05 -0500 Subject: [PATCH 1/3] init --- asm/README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/asm/README.md b/asm/README.md index 17078086..0429ae88 100644 --- a/asm/README.md +++ b/asm/README.md @@ -25,3 +25,5 @@ node asm source.asm ## TODO * Port this to Python + + \ No newline at end of file From 78ce64b5f565dd87e99f1d9210cb6eb3d2a956a9 Mon Sep 17 00:00:00 2001 From: Barbara Hunter Date: Wed, 6 Mar 2019 16:45:44 -0500 Subject: [PATCH 2/3] fix struct --- ls8/cpu.c | 4 +++- ls8/cpu.h | 12 +++++++++--- ls8/ls8.c | 9 +++++++-- 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/ls8/cpu.c b/ls8/cpu.c index bdb1f506..21e08766 100644 --- a/ls8/cpu.c +++ b/ls8/cpu.c @@ -1,7 +1,9 @@ #include "cpu.h" +#include +#include #define DATA_LEN 6 - +#define LR 7 /** * Load the binary bytes from a .ls8 source file into a RAM array */ diff --git a/ls8/cpu.h b/ls8/cpu.h index 46e49c44..64e58dee 100644 --- a/ls8/cpu.h +++ b/ls8/cpu.h @@ -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 @@ -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 diff --git a/ls8/ls8.c b/ls8/ls8.c index d24578ee..00ab6b7e 100644 --- a/ls8/ls8.c +++ b/ls8/ls8.c @@ -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; From 0e1e806704710f7e63865fc0cda96a168f9c521b Mon Sep 17 00:00:00 2001 From: Barbara Hunter Date: Thu, 7 Mar 2019 17:13:47 -0500 Subject: [PATCH 3/3] switch --- ls8/cpu.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/ls8/cpu.c b/ls8/cpu.c index 21e08766..4b81e3b7 100644 --- a/ls8/cpu.c +++ b/ls8/cpu.c @@ -52,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); }