-
Notifications
You must be signed in to change notification settings - Fork 6
Computer-Architecture-Sumi👩💻 #210
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
sumi419
wants to merge
9
commits into
bloominstituteoftechnology:master
Choose a base branch
from
sumi419: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
Changes from 4 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
e96be11
PR
sumi419 d39f908
CPU struct 👩💻
sumi419 9e9ba3a
cpu c
sumi419 7a6cd58
switch statement added
sumi419 5c8e37b
stack added 🥞
sumi419 5b560d1
Alu commands and flag added 🏳️🌈
sumi419 08ddf0a
added sprint test file here
sumi419 c27a6e1
updated sprint test and removed comments
sumi419 67ec141
mvp printing 1,4,5 🎉
sumi419 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 | ||
| } | ||
|
|
@@ -47,21 +49,75 @@ 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; | ||
|
|
||
| // 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 might want to make this more verbose and include the operation code and the program counter so you can debug better. |
||
| 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.
Awesome job with this! 🎉
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 just need to make use of this variable.