Skip to content
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

Thurs build #277

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Call and RET added
  • Loading branch information
Simone Ballard committed Apr 16, 2021
commit 266fda20f5031cbf2ac0b3510602fc047af8c423
18 changes: 16 additions & 2 deletions ls8/cpu.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
MUL = 0b10100010
PUSH = 8
POP = 10
CALL = 11
RET = 12

class CPU:
"""Main CPU class."""
Expand Down Expand Up @@ -114,7 +116,7 @@ def run(self):
self.alu("MUL", self.reg_a, self.reg_b)
#PUSH
elif instruction == PUSH:
reg_index = self.ram[self.pc + 1]
reg_index = operand_a
val = self.registers[reg_index]

self.registers[self.sp] -= 1
Expand All @@ -123,7 +125,7 @@ def run(self):
self.pc += 2
#POP
elif instruction == POP:
reg_index = self.ram[self.pc + 1]
reg_index = operand_a

self.registers[reg_index] = self.ram[self.registers[self.sp]]

Expand All @@ -133,6 +135,18 @@ def run(self):
elif instruction == HLT:
running = False
sys.exit(0)
# CALL
elif instruction == CALL:
address_to_return_to = self.pc + 2
self.registers[self.pc] -= 1
self.ram[self.registers[self.sp]] = address_to_return_to
reg_index = operand_a
address_to_call = self.registers[reg_index]
self.pc = address_to_call

elif instruction == RET:
self.pc = self.ram[self.registers[self.sp]]
self.registers[self.sp] + 1

else:
print("WRONG WAY")
Expand Down