Skip to content

Commit 2ca4f4a

Browse files
committed
Updating ahead of clock speed assignment
1 parent 9580d91 commit 2ca4f4a

File tree

3 files changed

+29
-19
lines changed

3 files changed

+29
-19
lines changed

src/lpc/cmd.py

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -58,20 +58,22 @@ def visit_def(node):
5858

5959
class Commands:
6060

61-
def __init__(self):
61+
def __init__(self, show_speed: bool = False):
6262
self._syntax = {
63-
"1": self.__add,
64-
"2": self.__sub,
65-
"3": self.__sta,
66-
"4": self.__sft,
67-
"5": self.__lda,
68-
"6": self.__bra,
69-
"7": self.__brz,
70-
"8": self.__brp,
71-
"901": self.__inp,
72-
"902": self.__out,
73-
"0": self.__hlt
63+
"1": {"cmd": self.__add, "cycles": 3},
64+
"2": {"cmd": self.__sub, "cycles": 3},
65+
"3": {"cmd":self.__sta, "cycles": 1},
66+
"4": {"cmd": self.__sft, "cycles": 0},
67+
"5": {"cmd": self.__lda, "cycles": 2},
68+
"6": {"cmd": self.__bra, "cycles": 2},
69+
"7": {"cmd": self.__brz, "cycles": 2},
70+
"8": {"cmd": self.__brp, "cycles": 2},
71+
"901": {"cmd": self.__inp, "cycles": 1},
72+
"902": {"cmd": self.__out, "cycles": 1},
73+
"0": {"cmd": self.__hlt, "cycles": 0}
7474
}
75+
self._show_speed = show_speed
76+
self._total_clock = 0
7577

7678
def parse(self, **kwargs):
7779
try:
@@ -85,7 +87,8 @@ def parse(self, **kwargs):
8587
if self._arg == "9":
8688
self._arg = kwargs['arg']
8789
try:
88-
return self._syntax[self._arg]
90+
self._total_clock += self._syntax[self._arg]["cycles"]
91+
return self._syntax[self._arg]["cmd"]
8992
except:
9093
print(f"[ERROR] Invalid instruction at line {self._line}.")
9194
return
@@ -99,6 +102,8 @@ def __add(self, acc, storage) -> int:
99102
def __sub(self, acc, storage) -> int:
100103
sub = int(storage._spaces[self._val])
101104
acc.value -= sub
105+
if acc.value < 0:
106+
acc.value = 0
102107

103108
@storage
104109
def __sta(self, acc, storage):
@@ -147,6 +152,7 @@ def __sft(self, acc, storage):
147152

148153
@inputs
149154
def __inp(self, acc, storage, input: int = 0):
155+
storage._expected_inputs += 1
150156
try:
151157
int(input)
152158
if input > 999:
@@ -162,5 +168,7 @@ def __out(self, acc, storage):
162168

163169
@halt
164170
def __hlt(self, acc, storage):
171+
if self._show_speed:
172+
print(self._total_clock)
165173
sys.exit(0)
166174
return False

src/lpc/main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def main() -> None:
2929
print("Invalid source file.")
3030
sys.exit(1)
3131
with open(src, "r") as fh:
32-
data = [val.strip() for val in fh.readlines()]
32+
data = [val.strip() for val in fh.readlines() if val.strip()]
3333

3434
# Initialize accumulator
3535
acc = Accumulator()
@@ -42,7 +42,7 @@ def main() -> None:
4242
debug_log(acc, storage)
4343

4444
# Prepare the ISA
45-
commands = Commands()
45+
commands = Commands(cliarg.optional.speed)
4646

4747
# Get inputs from command line API, should
4848
# convert to a tuple if supplied with

src/lpc/parts.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def __init__(self, instructions):
1010
self._counter = 1
1111
self._program = (
1212
re.split(
13-
r"\s{2,}|\t{2,}",
13+
r"\s{2,}|\t{1,}",
1414
instruction
1515
) for instruction in instructions)
1616
self._expected_inputs = 0
@@ -25,8 +25,8 @@ def __initialize_storage(self):
2525
self._spaces.append(None)
2626
for instruction in self._program:
2727
self._spaces[int(instruction[0])] = instruction[1]
28-
if instruction[1] == "901":
29-
self._expected_inputs += 1
28+
#if instruction[1] == "901":
29+
# self._expected_inputs += 1
3030
try:
3131
comment = str(instruction[2])
3232
if not comment.startswith("@"):
@@ -60,6 +60,8 @@ def value(self):
6060
def value(self, value):
6161
value = int(value)
6262
self._value = int(self._value)
63+
if self._value < 0:
64+
self._value = 0
6365
if self._value > 9999:
6466
print("[ERROR] ACCUMULATOR OVERFLOW!")
6567
sys.exit(1)
@@ -68,7 +70,7 @@ def value(self, value):
6870
self._value = value
6971

7072
@value.getter
71-
def x(self):
73+
def value(self):
7274
return self._value
7375

7476
class Inputs:

0 commit comments

Comments
 (0)