-
Notifications
You must be signed in to change notification settings - Fork 6
first commit #208
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
DavidBenavidez123
wants to merge
2
commits into
bloominstituteoftechnology:master
Choose a base branch
from
DavidBenavidez123: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
first commit #208
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 |
|---|---|---|
|
|
@@ -8,18 +8,19 @@ | |
| void cpu_load(struct cpu *cpu) | ||
| { | ||
| char data[DATA_LEN] = { | ||
| // From print8.ls8 | ||
| 0b10000010, // LDI R0,8 | ||
| 0b00000000, | ||
| 0b00001000, | ||
| 0b01000111, // PRN R0 | ||
| 0b00000000, | ||
| 0b00000001 // HLT | ||
| // From print8.ls8 | ||
| 0b10000010, // LDI R0,8 | ||
| 0b00000000, | ||
| 0b00001000, | ||
| 0b01000111, // PRN R0 | ||
| 0b00000000, | ||
| 0b00000001 // HLT | ||
| }; | ||
|
|
||
| int address = 0; | ||
|
|
||
| for (int i = 0; i < DATA_LEN; i++) { | ||
| for (int i = 0; i < DATA_LEN; i++) | ||
| { | ||
| cpu->ram[address++] = data[i]; | ||
| } | ||
|
|
||
|
|
@@ -31,10 +32,11 @@ void cpu_load(struct cpu *cpu) | |
| */ | ||
| void alu(struct cpu *cpu, enum alu_op op, unsigned char regA, unsigned char regB) | ||
| { | ||
| switch (op) { | ||
| case ALU_MUL: | ||
| // TODO | ||
| break; | ||
| switch (op) | ||
| { | ||
| case ALU_MUL: | ||
| // TODO | ||
| break; | ||
|
|
||
| // TODO: implement more ALU ops | ||
| } | ||
|
|
@@ -43,25 +45,92 @@ void alu(struct cpu *cpu, enum alu_op op, unsigned char regA, unsigned char regB | |
| /** | ||
| * Run the CPU | ||
| */ | ||
|
|
||
| void push_to_stack(struct cpu *cpu, int val) | ||
| { | ||
| cpu->reg[7]--; | ||
| cpu->ram[cpu->reg[7]] = val; | ||
| } | ||
| unsigned char cpu_pop(struct cpu *cpu) | ||
| { | ||
| unsigned char val = cpu_ram_read(cpu, cpu->reg[7]); | ||
| cpu->reg[7]++; | ||
| return val; | ||
| } | ||
|
|
||
| void cpu_run(struct cpu *cpu) | ||
| { | ||
| int running = 1; // True until we get a HLT instruction | ||
|
|
||
| while (running) { | ||
| 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 | ||
| //IR gives you the current instuction | ||
| //reading from the bitwise right shift will give you the number of operands: | ||
| unsigned char operandA = 0; | ||
| unsigned char operandB = 0; | ||
| unsigned int numOp = IR >> 6; | ||
|
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. Awesome job with this! Now you need to use it. |
||
|
|
||
| // 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: | ||
| // change the value of reg @ operanA to operandB; | ||
| 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"); | ||
|
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. You can make this error more verbose with the program counter and instruction code for better debugging. |
||
| 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); | ||
| } | ||
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.
Now you need to add PUSH and POP instructions to your switch statement.