Skip to content

Commit

Permalink
Many bugfixes and feature additions
Browse files Browse the repository at this point in the history
-Fixed xor
-Made program auto read from given file name
-Made registers print after instruction completes
-Made debug mode print instruction address
-Made bouncing ball example script
-Made assembler flip unsigned to signed values for LDI instructions
-Made assembler clone missing operands if necessary
-Fixed extra lines being counted when removing function names
  • Loading branch information
LightslicerGP committed Apr 14, 2024
1 parent ea9fb60 commit e438acd
Show file tree
Hide file tree
Showing 8 changed files with 490 additions and 38 deletions.
60 changes: 46 additions & 14 deletions Assembler.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,9 @@
jump_name = line.split(":")[0]
jumps[jump_name] = line_number
line = ""
else:
line_number += 1
main_file.write(line)
line_number += 1
print("jumps:", jumps) if debug else None

# Replace all constants names with their values
Expand Down Expand Up @@ -108,6 +109,36 @@
line = line.upper()
main_file.write(line)

# change unsigned numbers to signed for ldi
print("changing unsigned numbers to signed for ldi") if debug else None
with open("Instructions Compiled", "r") as instruction_file:
lines = instruction_file.readlines()
with open("Instructions Compiled", "w") as main_file:
for line in lines:
if line.startswith("LDI"):
if 256 > int(line.split()[2]) > 127:
line = line.replace(line.split()[2], str(-(256 - int(line.split()[2]))))
main_file.write(line)


# fill missing operands
print("filling missing operands") if debug else None
with open("Instructions Compiled", "r") as instruction_file:
lines = instruction_file.readlines()
with open("Instructions Compiled", "w") as main_file:
for line in lines:
if line.startswith(("INC", "DEC", "RSH", "LSH", "INV", "LOD", "STR")): # two operands
if len(line.split()) == 2: # one opcode, only one operand
fragments = line.split()
fragments.append(fragments[1])
line = " ".join(fragments) + "\n"
elif line.startswith(("ADD","SUB","MLT","DVS","SQA","SQR","ORR","AND","XOR")): # three operands
if len(line.split()) == 3: # one opcode, only 2 operands
fragments = line.split()
fragments.append(fragments[1])
line = " ".join(fragments) + "\n"
main_file.write(line)


# remove last line
print("removing last line") if debug else None
Expand All @@ -117,19 +148,20 @@
main_file.write(lines.rstrip("\n"))


# print array
print("printing array") if debug else None
with open("Instructions Compiled", "r") as instruction_file:
lines = instruction_file.readlines()
print("\n")
instruction_array = [
tuple(
int(item) if re.match(r"^-?\d+$", item) else item
for item in line.strip().split()
)
for line in lines
]
print(str(instruction_array) + "\n")
# got rid because its built into the cpu now
# # print array
# print("printing array") if debug else None
# with open("Instructions Compiled", "r") as instruction_file:
# lines = instruction_file.readlines()
# print("\n")
# instruction_array = [
# tuple(
# int(item) if re.match(r"^-?\d+$", item) else item
# for item in line.strip().split()
# )
# for line in lines
# ]
# print(str(instruction_array) + "\n")


# # sample title
Expand Down
25 changes: 20 additions & 5 deletions Excpute.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# import Operations
import threading
import pygame
import re
import Display
import RAM
import Port
import RAM

registers = [0] * 8

Expand Down Expand Up @@ -182,7 +183,7 @@ def xor_op(regA, regB, regDest):
print(f"{instruction_address}: Xor")
A = reg_read(regA, False) # THIS IS FALSE because
B = reg_read(regB, False) # its not math, just logic
result = A & B
result = A ^ B
reg_write(regDest, result, False)
next_instruction()

Expand Down Expand Up @@ -469,16 +470,30 @@ def spd_op(regA, property):
"SPD": lambda ra, pr: spd_op(ra, pr),
}

program = [('LDI', 2, -1), ('SPD', 2, 2), ('LDI', 0, 1), ('SPD', 0, 5), ('LDI', 1, -128), ('JMP', 0)]
def get_instructions(file):
with open(file, "r") as instruction_file:
lines = instruction_file.readlines()
instruction_array = [
tuple(
int(item) if re.match(r"^-?\d+$", item) else item
for item in line.strip().split()
)
for line in lines
]
return instruction_array

program = get_instructions("Instructions Compiled")

while instruction_address < 256:
if debug or print_registers:
print(registers)
try:
instruction = program[instruction_address]
except IndexError:
print("Ran out of instructions, halted automatically")
if debug:
print("Instruction address:", instruction_address)
exit()
if debug or print_registers:
print(registers)
op = instruction[0].upper()
args = instruction[1:]

Expand Down
6 changes: 0 additions & 6 deletions Instructions Compiled

This file was deleted.

13 changes: 0 additions & 13 deletions Instructions Uncompiled

This file was deleted.

55 changes: 55 additions & 0 deletions Sample script sets/Bouncing Ball/DO NOT USE/Bouncing Ball Example
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Initialization
CAL init
loop:
CAL update # Update ball position and check for collisions
CAL draw # Draw the ball on the screen
JMP loop # Repeat the loop
# Subroutine to initialize variables
init:
LDI 0 0 # Set initial X position
LDI 1 10 # Set initial Y position
LDI 2 1 # Set X velocity (positive for rightward movement)
LDI 3 1 # Set Y velocity (positive for downward movement)
RTN
# Subroutine to update ball position and check for collisions
update:
ADD 0 0 2 # Update X position based on X velocity
ADD 1 1 3 # Update Y position based on Y velocity

# Check for X boundary collision (assuming screen width of 256)
LDI 4 255 # Load maximum X value (255 for 8-bit screen width)
CMP 0 4 # Compare current X with maximum X
JIZ 0 right_bounce # Jump to right_bounce if X is at the edge

LDI 4 0 # Load minimum X value (0)
CMP 0 4 # Compare current X with minimum X
JIZ 0 left_bounce # Jump to left_bounce if X is at the edge

# Check for Y boundary collision (assuming screen height of 256)
LDI 4 255 # Load maximum Y value (255 for 8-bit screen height)
CMP 1 4 # Compare current Y with maximum Y
JIZ 0 bottom_bounce# Jump to bottom_bounce if Y is at the edge

LDI 4 0 # Load minimum Y value (0)
CMP 1 4 # Compare current Y with minimum Y
JIZ 0 top_bounce # Jump to top_bounce if Y is at the edge
RTN
# Subroutines to handle bouncing on each edge
right_bounce:
LDI 2 -1 # Reverse X velocity
RTN
left_bounce:
LDI 2 1 # Reverse X velocity
RTN
bottom_bounce:
LDI 3 -1 # Reverse Y velocity
RTN
top_bounce:
LDI 3 1 # Reverse Y velocity
RTN
# Subroutine to draw the ball on the screen
draw:
SPD 0 2 # Set X coordinate for pixel
SPD 1 4 # Set Y coordinate for pixel
SPD 255 0 # Set pixel color to white (assuming 255 is white)
RTN
111 changes: 111 additions & 0 deletions Sample script sets/Bouncing Ball/DO NOT USE/Instructions UncompiledOld
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
xpos = 0
ypos = 1
xvel = 2
yvel = 3
r4 = 4
rSP = 5
rFlags = 6
rPC = 7

r_col = 0
g_col = 1
b_col = 2
x_prop = 3
y_prop = 4
setpixel = 5

on = -1
off = 0

init:
ldi xpos 1 # inital x pos
ldi ypos 10 # initial y pos
ldi xvel 1 # initial x vel
ldi yvel 1 # initial y vel

ldi r4 on # setting color
spd r4 r_col
spd r4 g_col
spd r4 b_col
cal display

loop:
cal update
cal display
jmp loop
update:
# x velocity
ldi r4 1 # checking for right movement
xor r4 xvel r4
jiz r4 increase_x
breakpointE:
ldi r4 -1 # checking for left movement
xor r4 xvel r4
jiz r4 decrease_x
breakpointF:
# y velocity
ldi r4 1 # checking for up movement
xor r4 yvel r4
jiz r4 increase_y
breakpointG:
ldi r4 -1 # checking for down movement
xor r4 yvel r4
jiz r4 decrease_y
breakpointH:
rtn
ldi r4 -1 # for right wall collision
xor r4 xpos r4
jiz r4 right_bounce
breakpointA:
ldi r4 0 # for left wall
xor r4 xpos r4
jiz r4 left_bounce
breakpointB:
ldi r4 -1 # for top wall
xor r4 ypos r4
jiz r4 top_bounce
breakpointC:
ldi r4 1 # for bottom wall
xor r4 ypos r4
jiz r4 bottom_bounce
breakpointD:
rtn

right_bounce:
ldi xvel -1
jmp breakpointA

left_bounce:
ldi xvel 1
jmp breakpointB

top_bounce:
ldi yvel -1
jmp breakpointC

bottom_bounce:
ldi yvel 1
jmp breakpointD

increase_x:
inc xpos xpos
jmp breakpointE

decrease_x:
dec xpos xpos
jmp breakpointF

increase_y:
inc ypos ypos
jmp breakpointG

decrease_y:
dec ypos ypos
jmp breakpointH


display:
spd xpos x_prop
spd ypos y_prop
spd 0 setpixel
rtn
Loading

0 comments on commit e438acd

Please sign in to comment.