Skip to content

Commit

Permalink
Fix test1
Browse files Browse the repository at this point in the history
  • Loading branch information
mole99 committed Apr 13, 2024
1 parent 7911e11 commit 4eb9bd3
Show file tree
Hide file tree
Showing 14 changed files with 50 additions and 41 deletions.
40 changes: 29 additions & 11 deletions sw/assembler.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,22 @@
}


def get_syntax(fmt):
def get_operands(fmt):
"""Return operand string"""

if fmt == 'immediate':
return 'IMMEDIATE'
elif fmt == 'single_operand':
return 'RA'
elif fmt == 'dual_operand':
return 'RA RB'
elif fmt == 'pseudo':
return ''
else:
return 'UNKNOWN'

def summary():
"""Print a summary of all instructions as Markdown table"""
categories = {}

for name, instruction in instructions.items():
Expand All @@ -73,7 +77,7 @@ def summary():

for name, instruction in category.items():

print(f'|{name} {get_syntax(instruction["format"])}|{instruction["short"]}|{instruction["description"]}|')
print(f'|{name} {get_operands(instruction["format"])}|{instruction["short"]}|{instruction["description"]}|')

def get_register(string):
if string[0] != 'R':
Expand All @@ -83,7 +87,9 @@ def get_register(string):
return int(string[1:])

def assemble(program, verbose=False):
assembled = ''
"""Assemble the shader into binary text format"""

assembled = []

# Remove all comments
program = os.linesep.join([s.split('#', 1)[0] for s in program.splitlines()])
Expand All @@ -102,13 +108,13 @@ def assemble(program, verbose=False):
instr = instructions[token[0]]

if instr['format'] == 'pseudo':
assembled += f'{instr["opcode"]} // {line}\n'
assembled.append(f'{instr["opcode"]} // {line}')

elif instr['format'] == 'immediate':
if (len(token) != 2):
print(f'Instruction {token[0]} expects one immediate')
imm = int(token[1])
assembled += f'{instr["opcode"]}_{imm:06b} // {line}\n'
assembled.append(f'{instr["opcode"]}_{imm:06b} // {line}')

elif instr['format'] == 'dual_operand':
if (len(token) != 3):
Expand All @@ -117,15 +123,15 @@ def assemble(program, verbose=False):
op0 = get_register(token[1])
op1 = get_register(token[2])

assembled += f'{instr["opcode"]}_{op1:02b}_{op0:02b} // {line}\n'
assembled.append(f'{instr["opcode"]}_{op1:02b}_{op0:02b} // {line}')

elif instr['format'] == 'single_operand':
if (len(token) != 2):
print(f'Instruction {token[0]} expects one operand')

op0 = get_register(token[1])

assembled += f'{instr["opcode"]}_{op0:02b} // {line}\n'
assembled.append(f'{instr["opcode"]}_{op0:02b} // {line}')

else:
print(f'Instruction format unknown: {instr["format"]}')
Expand All @@ -136,6 +142,7 @@ def assemble(program, verbose=False):
return assembled

def simulate(program, x_pos=0, y_pos=0, cur_time=0, user=0, verbose=False):
"""Simulate the shader program"""

register = [0, 0, 0, 0]
rgb = [0, 0, 0]
Expand Down Expand Up @@ -249,7 +256,6 @@ def simulate(program, x_pos=0, y_pos=0, cur_time=0, user=0, verbose=False):
if verbose:
print(f'register[0] {register[0]}')


if verbose:
print(f'Current state:')
print(f'register: {register}')
Expand Down Expand Up @@ -298,9 +304,11 @@ def main():
with open(args.input, 'r') as f:
shader = f.read()

NUM_INSTR = 10

if args.image:
WIDTH = 640//10
HEIGHT = 480//10
WIDTH = 640//NUM_INSTR
HEIGHT = 480//NUM_INSTR

from PIL import Image

Expand Down Expand Up @@ -334,6 +342,16 @@ def main():

assembled = assemble(shader, args.verbose)

# Fill up with nops

while len(assembled) < NUM_INSTR:
assembled.append('01_00_00_00 // NOP')

if len(assembled) > NUM_INSTR:
print('Error: Too many instruction!')

assembled = '\n'.join(assembled)

if args.verbose:
print(assembled)

Expand Down
4 changes: 2 additions & 2 deletions sw/binary/test1.bit
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
00_1110_00 // CLEAR R0
00_0000_00 // SETRGB R0
00_0100_01 // GETX R1
00_0101_10 // GETY R2
Expand All @@ -6,5 +7,4 @@
00_0000_01 // SETRGB R1
00_1000_10 // IFEQ R2
00_0000_10 // SETRGB R2
01_00_00_00 // NOP
01_00_00_00 // NOP
01_00_00_00 // NOP
2 changes: 1 addition & 1 deletion sw/binary/test2.bit
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
01_11_00_00 // XOR R0 R0
01_11_00_00 // XOR R0 R0
01_00_00_00 // NOP
01_00_00_00 // NOP
01_00_00_00 // NOP
2 changes: 2 additions & 0 deletions sw/binary/test3.bit
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@
01_11_00_00 // XOR R0 R0
01_11_00_00 // XOR R0 R0
01_11_00_00 // XOR R0 R0
01_00_00_00 // NOP
01_00_00_00 // NOP
2 changes: 2 additions & 0 deletions sw/binary/test4.bit
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@
01_11_00_00 // XOR R0 R0
01_11_00_00 // XOR R0 R0
01_11_00_00 // XOR R0 R0
01_00_00_00 // NOP
01_00_00_00 // NOP
2 changes: 2 additions & 0 deletions sw/binary/test5.bit
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@
00_1000_01 // IFEQ R1
11_000000 // LDI 0
00_0000_00 // SETRGB R0
01_00_00_00 // NOP
01_00_00_00 // NOP
2 changes: 2 additions & 0 deletions sw/binary/test6.bit
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@
00_1000_01 // IFEQ R1
11_000000 // LDI 0
00_0000_00 // SETRGB R0
01_00_00_00 // NOP
01_00_00_00 // NOP
2 changes: 1 addition & 1 deletion sw/binary/test7.bit
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
00_0101_01 // GETY R1
00_1010_01 // IFGE R1
00_0000_11 // SETRGB R3
01_00_00_00 // NOP
01_00_00_00 // NOP
10 changes: 4 additions & 6 deletions sw/shader/test1.shader
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# Hello!

# This is a test #213
# Hello World!
# Draw one vertical and one horizontal line
# at X=10, Y=10

CLEAR R0
SETRGB R0

GETX R1
Expand All @@ -14,6 +15,3 @@ SETRGB R1

IFEQ R2
SETRGB R2

NOP
NOP
8 changes: 2 additions & 6 deletions sw/shader/test2.shader
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
# Use sine to set the colors for r and g

GETX R0
SINE R1
SETR R1

GETY R0
SINE R1
SETG R1

XOR R0 R0
XOR R0 R0

NOP
NOP
6 changes: 0 additions & 6 deletions sw/shader/test3.shader
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,3 @@ SETR R0

GETY R1
SETG R1

XOR R0 R0
XOR R0 R0
XOR R0 R0
XOR R0 R0

7 changes: 2 additions & 5 deletions sw/shader/test4.shader
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
# Xor both coordinates and write to rgb

GETX R0
GETY R1

XOR R0 R1
SETRGB R0

XOR R0 R0
XOR R0 R0
XOR R0 R0
XOR R0 R0
2 changes: 0 additions & 2 deletions sw/shader/test7.shader
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,3 @@ GETY R1
# set color to white
IFGE R1
SETRGB R3

NOP
2 changes: 1 addition & 1 deletion test/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ async def test_vga_load(dut, shader_name='test7'):
await ClockCycles(dut.clk, 10)

tf = TestFactory(test_function=test_vga_load)
tf.add_option(name='shader_name', optionlist=['test1', 'test2', 'test7'])
tf.add_option(name='shader_name', optionlist=['test1', 'test2', 'test3', 'test4', 'test5', 'test6', 'test7'])
tf.generate_tests()

@cocotb.test(skip=os.environ.get('GL_TEST', None) != None)
Expand Down

0 comments on commit 4eb9bd3

Please sign in to comment.