-
Notifications
You must be signed in to change notification settings - Fork 6
Luis Martinez #205
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
luiscmartinez
wants to merge
10
commits into
bloominstituteoftechnology:master
Choose a base branch
from
luiscmartinez:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Luis Martinez #205
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
b231ac8
initial commit plus add short descriptions of what files do
luiscmartinez ff85396
implemented struct cpu in cpu.h
luiscmartinez e654bb0
implemented cpu_init
luiscmartinez c22bda4
refactored cpu_init && implemented cpu_rn
luiscmartinez b8baf84
implemented ls8 fun & duggin
luiscmartinez e4108f8
defined MUL & ADD
luiscmartinez 2f03dcb
added ALU op & implemented switch
luiscmartinez 41d465e
implemented cpu_load
luiscmartinez 43541fb
path added to argument
luiscmartinez 776203a
refactored files and variables
luiscmartinez File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| { | ||
| "workbench.colorCustomizations": { | ||
| "activityBar.background": "#033615", | ||
| "titleBar.activeBackground": "#044B1D", | ||
| "titleBar.activeForeground": "#F0FEF5" | ||
| }, | ||
| "files.associations": { | ||
| "*.js": "javascriptreact", | ||
| "cpu.h": "c", | ||
| "stdlib.h": "c" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,42 +1,70 @@ | ||
| #include <stdio.h> | ||
| #include <stdlib.h> | ||
| #include <string.h> | ||
| #include "cpu.h" | ||
|
|
||
| #define DATA_LEN 6 | ||
|
|
||
| /** | ||
| * Reads from a CPU struct's RAM array | ||
| */ | ||
| 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 *path) | ||
| { | ||
| char data[DATA_LEN] = { | ||
| // From print8.ls8 | ||
| 0b10000010, // LDI R0,8 | ||
| 0b00000000, | ||
| 0b00001000, | ||
| 0b01000111, // PRN R0 | ||
| 0b00000000, | ||
| 0b00000001 // HLT | ||
| }; | ||
| FILE *fileptr = fopen(path, "r"); | ||
|
|
||
| if (fileptr == NULL) | ||
| { | ||
| printf("Couldn't open program: %s\n", path); | ||
| exit(2); | ||
| } | ||
|
|
||
| int address = 0; | ||
| char line[8000]; | ||
|
|
||
| for (int i = 0; i < DATA_LEN; i++) { | ||
| cpu->ram[address++] = data[i]; | ||
| while (fgets(line, sizeof(line), fileptr) != NULL) | ||
| { | ||
| char *endptr; | ||
| // & 0xFF gives you the last 8 bits of the ul (so it will always fit in an unsigned char) | ||
| unsigned char value = strtoul(line, &endptr, 2) & 0xFF; | ||
| if (endptr == line) | ||
| { | ||
| continue; | ||
| } | ||
| cpu_ram_write(cpu, address++, value); | ||
| } | ||
|
|
||
| // TODO: Replace this with something less hard-coded | ||
| fclose(fileptr); | ||
| } | ||
|
|
||
| /** | ||
| * ALU | ||
| */ | ||
| void alu(struct cpu *cpu, enum alu_op op, unsigned char regA, unsigned char regB) | ||
| { | ||
| switch (op) { | ||
| case ALU_MUL: | ||
| // TODO | ||
| break; | ||
| unsigned int a = cpu->registers[regA]; | ||
| unsigned int b = cpu->registers[regB]; | ||
|
|
||
| // TODO: implement more ALU ops | ||
| switch (op) | ||
| { | ||
| case ALU_MUL: | ||
| cpu->registers[regA] = a * b; | ||
| break; | ||
| case ALU_ADD: | ||
| cpu->registers[regA] = a + b; | ||
| break; | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -47,14 +75,46 @@ void cpu_run(struct cpu *cpu) | |
| { | ||
| int running = 1; // True until we get a HLT instruction | ||
|
|
||
| while (running) { | ||
| // TODO | ||
| while (running) | ||
| { | ||
| // 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 | ||
| // Not entirely needed if we use a switch statement | ||
| // 3. Get the appropriate value(s) of the operands following this instruction | ||
| // Operations may need up to 2 bytes after PC | ||
| 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. | ||
| // 5. Do whatever the instruction should do according to the spec. | ||
| // 6. Move the PC to the next instruction. | ||
| switch (ir) | ||
| { | ||
| case LDI: | ||
| // Set the value of a register to an integer | ||
| cpu->registers[operandA] = operandB; | ||
| cpu->pc += 3; | ||
| break; | ||
| case PRN: | ||
| // Print numeric value stored in the given register | ||
| printf("%d\n", cpu->registers[operandA]); | ||
| cpu->pc += 2; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Eventually you might come across a way to increase the PC using the instruction number itself (hint: this uses bit shifting). |
||
| break; | ||
| case MUL: | ||
| alu(cpu, ALU_MUL, operandA, operandB); | ||
| cpu->pc += 3; | ||
| break; | ||
| case ADD: | ||
| alu(cpu, ALU_ADD, operandA, operandB); | ||
| cpu->pc += 3; | ||
| break; | ||
| case HLT: | ||
| // Halt the CPU (and exit the emulator) | ||
| running = 0; | ||
| break; | ||
| default: | ||
| break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -63,5 +123,8 @@ 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->ram, 0, sizeof(cpu->ram)); | ||
| memset(cpu->registers, 0, sizeof(cpu->registers)); | ||
| cpu->registers[7] = 0xF4; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking good!