Skip to content
Open
Show file tree
Hide file tree
Changes from 7 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: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Computer Architecture
# Computer Architecture -

## Project

Expand Down
95 changes: 93 additions & 2 deletions ls8/cpu.c
Original file line number Diff line number Diff line change
@@ -1,31 +1,81 @@
#include "cpu.h"
#include "string.h"
#include "stdio.h"

#define DATA_LEN 6
#define DATA_LEN 12

FILE *fp;

/**
* 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 *program)
{

/*
char data[DATA_LEN] = {

// From print8.ls8
0b10000010, // LDI R0,8
0b00000000,
0b00001000,
0b01000111, // PRN R0
0b00000000,
0b00000001 // HLT


// Testing Mult instruction
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Great you're commenting out the instructions and the switch to the registers. A rather cumbersome process but I hope it's demystifying the "black box for you."

0b10000010, // LDI R0,8
0b00000000,
0b00001000,
0b10000010, // LDI R1,9
0b00000001,
0b00001001,
0b10100010, // MUL R0,R1
0b00000000,
0b00000001,
0b01000111, // PRN R0
0b00000000,
0b00000001, // HLT


};

int address = 0;

for (int i = 0; i < DATA_LEN; i++) {
cpu->ram[address++] = data[i];
}
*/

unsigned char line[8];

fp = fopen(program, "r");

if( fp == NULL) {
fprintf(stderr, "Error opening file.\n");
}

int i;
while( fgets(line, 8, fp) != NULL ) {
cpu_ram_write(cpu, i, line);
}

fclose(fp);

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

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

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

/**
* ALU
*/
Expand All @@ -47,14 +97,49 @@ void cpu_run(struct cpu *cpu)
{
int running = 1; // True until we get a HLT instruction

//int PC = cpu->PC;
//unsigned char value;

while (running) {
// TODO
// 1. Get the value of the current instruction (in address PC).
//unsigned char instruction = cpu->ram[PC];

unsigned char instruction = cpu_ram_read(cpu, cpu->PC);
// 2. Figure out how many operands this next instruction requires
unsigned char operandA = cpu_ram_read(cpu, cpu->PC + 1);
unsigned char operandB = cpu_ram_read(cpu, cpu->PC + 2);

//bitwise right shift

//int num_operands = instruction[0];

// 3. Get the appropriate value(s) of the operands following this instruction
// 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 HLT:
running = 0;
break;

case LDI:
cpu->registers[operandA] = operandB;
cpu->PC += 3;
break;

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

case MUL:
cpu->registers[operandA] = (cpu->registers[operandA] * cpu->registers[operandB]);
cpu->PC += 2;
break;

}
}
}

Expand All @@ -64,4 +149,10 @@ void cpu_run(struct cpu *cpu)
void cpu_init(struct cpu *cpu)
{
// TODO: Initialize the PC and other special registers
cpu->PC = 0;

memset(cpu->registers, 0, 8*sizeof(unsigned char));

memset(cpu->ram, 0, 8*sizeof(unsigned char));

}
6 changes: 5 additions & 1 deletion ls8/cpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
struct cpu {
// TODO
// PC
int PC;
// registers (array)
unsigned char registers[8];
// ram (array)
unsigned char ram[8];
};

// ALU operations
Expand All @@ -23,11 +26,12 @@ enum alu_op {
#define LDI 0b10000010
#define HLT 0b00000001
#define PRN 0b01000111
#define MUL 0b10100010
// 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 *program);
extern void cpu_init(struct cpu *cpu);
extern void cpu_run(struct cpu *cpu);

Expand Down
24 changes: 20 additions & 4 deletions ls8/ls8.c
Original file line number Diff line number Diff line change
@@ -1,16 +1,32 @@
#include <stdio.h>
#include "cpu.h"
#include <stdlib.h>

/**
* Main
*/
int main(void)
int main(int argc, char **argv)
{
struct cpu cpu;
char *program;

cpu_init(&cpu);
cpu_load(&cpu);
cpu_run(&cpu);
if (argc < 2) {
fprintf(stderr, "Please specify which program the LS8 should run.\nUsage: ls8 program\n");
exit(1);

} else {
program = argv[1];

printf("%s\n", program);

cpu_init(&cpu);
cpu_load(&cpu, program);
cpu_run(&cpu);
}

//cpu_init(&cpu);
//cpu_load(&cpu);
//cpu_run(&cpu);

return 0;
}