diff --git a/sw/assembler.py b/sw/assembler.py index e322d18..3377359 100644 --- a/sw/assembler.py +++ b/sw/assembler.py @@ -45,7 +45,8 @@ } -def get_syntax(fmt): +def get_operands(fmt): + """Return operand string""" if fmt == 'immediate': return 'IMMEDIATE' @@ -53,10 +54,13 @@ def get_syntax(fmt): 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(): @@ -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': @@ -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()]) @@ -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): @@ -117,7 +123,7 @@ 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): @@ -125,7 +131,7 @@ def assemble(program, verbose=False): 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"]}') @@ -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] @@ -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}') @@ -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 @@ -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) diff --git a/sw/binary/test1.bit b/sw/binary/test1.bit index dcc2e38..ccc9bfa 100644 --- a/sw/binary/test1.bit +++ b/sw/binary/test1.bit @@ -1,3 +1,4 @@ +00_1110_00 // CLEAR R0 00_0000_00 // SETRGB R0 00_0100_01 // GETX R1 00_0101_10 // GETY R2 @@ -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 \ No newline at end of file diff --git a/sw/binary/test2.bit b/sw/binary/test2.bit index badf176..dfe79d9 100644 --- a/sw/binary/test2.bit +++ b/sw/binary/test2.bit @@ -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 \ No newline at end of file diff --git a/sw/binary/test3.bit b/sw/binary/test3.bit index 5c7be98..56035b3 100644 --- a/sw/binary/test3.bit +++ b/sw/binary/test3.bit @@ -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 \ No newline at end of file diff --git a/sw/binary/test4.bit b/sw/binary/test4.bit index 0a95dea..d2b5785 100644 --- a/sw/binary/test4.bit +++ b/sw/binary/test4.bit @@ -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 \ No newline at end of file diff --git a/sw/binary/test5.bit b/sw/binary/test5.bit index c1028d5..8331c0a 100644 --- a/sw/binary/test5.bit +++ b/sw/binary/test5.bit @@ -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 \ No newline at end of file diff --git a/sw/binary/test6.bit b/sw/binary/test6.bit index 94dd540..98d1250 100644 --- a/sw/binary/test6.bit +++ b/sw/binary/test6.bit @@ -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 \ No newline at end of file diff --git a/sw/binary/test7.bit b/sw/binary/test7.bit index bbe6dc3..8369cc9 100644 --- a/sw/binary/test7.bit +++ b/sw/binary/test7.bit @@ -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 \ No newline at end of file diff --git a/sw/shader/test1.shader b/sw/shader/test1.shader index 18faa95..4077612 100644 --- a/sw/shader/test1.shader +++ b/sw/shader/test1.shader @@ -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 @@ -14,6 +15,3 @@ SETRGB R1 IFEQ R2 SETRGB R2 - -NOP -NOP diff --git a/sw/shader/test2.shader b/sw/shader/test2.shader index 863aeaf..8c0d06c 100644 --- a/sw/shader/test2.shader +++ b/sw/shader/test2.shader @@ -1,3 +1,5 @@ +# Use sine to set the colors for r and g + GETX R0 SINE R1 SETR R1 @@ -5,9 +7,3 @@ SETR R1 GETY R0 SINE R1 SETG R1 - -XOR R0 R0 -XOR R0 R0 - -NOP -NOP diff --git a/sw/shader/test3.shader b/sw/shader/test3.shader index fc1b094..a5d835c 100644 --- a/sw/shader/test3.shader +++ b/sw/shader/test3.shader @@ -3,9 +3,3 @@ SETR R0 GETY R1 SETG R1 - -XOR R0 R0 -XOR R0 R0 -XOR R0 R0 -XOR R0 R0 - diff --git a/sw/shader/test4.shader b/sw/shader/test4.shader index cf0e0f6..2f5dcb9 100644 --- a/sw/shader/test4.shader +++ b/sw/shader/test4.shader @@ -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 diff --git a/sw/shader/test7.shader b/sw/shader/test7.shader index ba31f7e..3a9e355 100644 --- a/sw/shader/test7.shader +++ b/sw/shader/test7.shader @@ -21,5 +21,3 @@ GETY R1 # set color to white IFGE R1 SETRGB R3 - -NOP diff --git a/test/test.py b/test/test.py index e5f3a7b..4f73e77 100644 --- a/test/test.py +++ b/test/test.py @@ -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)