WIP web interface

This commit is contained in:
SergioFLS 2024-08-13 15:37:43 -04:00
parent bbe02cc431
commit 98b7e9e80f
2 changed files with 136 additions and 90 deletions

178
dis.py
View file

@ -50,93 +50,91 @@ def determine_instruction_type(opcode: int):
else: else:
return InstructionTypes.UNKNOWN return InstructionTypes.UNKNOWN
OP = [int(argv[1],16), 0] def disassemble(opcode: list):
if len(argv) >= 3: match determine_instruction_type(opcode[0]):
OP[1] = int(argv[2],16) case InstructionTypes.DATAPROCESSING_ALU6IMM:
match determine_instruction_type(OP[0]): alu_op_fmt = {
case InstructionTypes.DATAPROCESSING_ALU6IMM: 0b0000: 'R{0} += {1};',
alu_op_fmt = { 0b0001: 'R{0} += {1}, Carry;',
0b0000: 'R{0} += {1};', 0b0010: 'R{0} -= {1};',
0b0001: 'R{0} += {1}, Carry;', 0b0011: 'R{0} -= {1}, Carry;',
0b0010: 'R{0} -= {1};', 0b0100: 'CMP R{0}, {1};',
0b0011: 'R{0} -= {1}, Carry;', 0b0110: 'R{0} =- {1};',
0b0100: 'CMP R{0}, {1};', 0b1000: 'R{0} ^= {1};',
0b0110: 'R{0} =- {1};', 0b1001: 'R{0} = {1};',
0b1000: 'R{0} ^= {1};', 0b1010: 'R{0} |= {1};',
0b1001: 'R{0} = {1};', 0b1011: 'R{0} &= {1};',
0b1010: 'R{0} |= {1};', 0b1100: 'TEST R{0}, {1};',
0b1011: 'R{0} &= {1};', }
0b1100: 'TEST R{0}, {1};', alu_op = (opcode[0] & 0b1111_000_000_000000) >> 12
} rd = (opcode[0] & 0b0000_111_000_000000) >> 9
alu_op = (OP[0] & 0b1111_000_000_000000) >> 12 im6 = opcode[0] & 0b0000_000_000_111111
rd = (OP[0] & 0b0000_111_000_000000) >> 9 return alu_op_fmt[alu_op].format(rd, im6)
im6 = OP[0] & 0b0000_000_000_111111 case InstructionTypes.DATAPROCESSING_ALU6DIR:
print(alu_op_fmt[alu_op].format(rd, im6)) alu_op_fmt = {
case InstructionTypes.DATAPROCESSING_ALU6DIR: 0b0000: 'R{0} += [{1}];',
alu_op_fmt = { 0b0001: 'R{0} += [{1}], Carry;',
0b0000: 'R{0} += [{1}];', 0b0010: 'R{0} -= [{1}];',
0b0001: 'R{0} += [{1}], Carry;', 0b0011: 'R{0} -= [{1}], Carry;',
0b0010: 'R{0} -= [{1}];', 0b0100: 'CMP R{0}, [{1}];',
0b0011: 'R{0} -= [{1}], Carry;', 0b0110: 'R{0} =- [{1}];',
0b0100: 'CMP R{0}, [{1}];', 0b1000: 'R{0} ^= [{1}];',
0b0110: 'R{0} =- [{1}];', 0b1001: 'R{0} = [{1}];',
0b1000: 'R{0} ^= [{1}];', 0b1010: 'R{0} |= [{1}];',
0b1001: 'R{0} = [{1}];', 0b1011: 'R{0} &= [{1}];',
0b1010: 'R{0} |= [{1}];', 0b1100: 'TEST R{0}, [{1}];',
0b1011: 'R{0} &= [{1}];', }
0b1100: 'TEST R{0}, [{1}];', alu_op = (opcode[0] & 0b1111_000_000_000000) >> 12
} rd = (opcode[0] & 0b0000_111_000_000000) >> 9
alu_op = (OP[0] & 0b1111_000_000_000000) >> 12 a6 = opcode[0] & 0b0000_000_000_111111
rd = (OP[0] & 0b0000_111_000_000000) >> 9 return alu_op_fmt[alu_op].format(rd, a6)
a6 = OP[0] & 0b0000_000_000_111111 case InstructionTypes.DATAPROCESSING_SHFT:
print(alu_op_fmt[alu_op].format(rd, a6)) shift_op_fmt = {
case InstructionTypes.DATAPROCESSING_SHFT: 0b000: '',
shift_op_fmt = { 0b001: 'ASR',
0b000: '', 0b010: 'LSL',
0b001: 'ASR', 0b011: 'LSR',
0b010: 'LSL', 0b100: 'ROL',
0b011: 'LSR', 0b101: 'ROR',
0b100: 'ROL', }
0b101: 'ROR', alu_op_fmt = {
} 0b0000: 'R{0} += R{1} {2} {3};',
alu_op_fmt = { 0b0001: 'R{0} += R{1} {2} {3}, Carry;',
0b0000: 'R{0} += R{1} {2} {3};', 0b0010: 'R{0} -= R{1} {2} {3};',
0b0001: 'R{0} += R{1} {2} {3}, Carry;', 0b0011: 'R{0} -= R{1} {2} {3}, Carry;',
0b0010: 'R{0} -= R{1} {2} {3};', 0b0100: 'CMP R{0}, R{1} {2} {3};',
0b0011: 'R{0} -= R{1} {2} {3}, Carry;', 0b0110: 'R{0} =- R{1} {2} {3};',
0b0100: 'CMP R{0}, R{1} {2} {3};', 0b1000: 'R{0} ^= R{1} {2} {3};',
0b0110: 'R{0} =- R{1} {2} {3};', 0b1001: 'R{0} = R{1} {2} {3};',
0b1000: 'R{0} ^= R{1} {2} {3};', 0b1010: 'R{0} |= R{1} {2} {3};',
0b1001: 'R{0} = R{1} {2} {3};', 0b1011: 'R{0} &= R{1} {2} {3};',
0b1010: 'R{0} |= R{1} {2} {3};', 0b1100: 'TEST R{0}, R{1} {2} {3};',
0b1011: 'R{0} &= R{1} {2} {3};', }
0b1100: 'TEST R{0}, R{1} {2} {3};', alu_op = (opcode[0] & 0b1111_000_0_000_00_000) >> 12
} rd = (opcode[0] & 0b0000_111_0_000_00_000) >> 9
alu_op = (OP[0] & 0b1111_000_0_000_00_000) >> 12 shift_op = (opcode[0] & 0b0000_000_0_111_00_000) >> 5
rd = (OP[0] & 0b0000_111_0_000_00_000) >> 9 n = (opcode[0] & 0b0000_000_0_000_11_000) >> 3
shift_op = (OP[0] & 0b0000_000_0_111_00_000) >> 5 rs = opcode[0] & 0b0000_000_0_000_00_111
n = (OP[0] & 0b0000_000_0_000_11_000) >> 3 return alu_op_fmt[alu_op].format(rd, rs, shift_op_fmt[shift_op], n+1)
rs = OP[0] & 0b0000_000_0_000_00_111 case InstructionTypes.INTERRUPT:
print(alu_op_fmt[alu_op].format(rd, rs, shift_op_fmt[shift_op], n+1)) irq = (opcode[0] & 0b01) != 0
case InstructionTypes.INTERRUPT: fiq = (opcode[0] & 0b10) != 0
irq = (OP[0] & 0b01) != 0 if irq and not fiq:
fiq = (OP[0] & 0b10) != 0 return 'INT IRQ;'
if irq and not fiq: elif not irq and fiq:
print('INT IRQ;') return 'INT FIQ;'
elif not irq and fiq: elif irq and fiq:
print('INT FIQ;') return 'INT FIQ,IRQ;'
elif irq and fiq: else:
print('INT FIQ,IRQ;') return 'INT OFF;'
else: case InstructionTypes.FUNCTION_RETF:
print('INT OFF;') return 'RETF;'
case InstructionTypes.FUNCTION_RETF: case InstructionTypes.FUNCTION_RETI:
print('RETF;') return 'RETI;'
case InstructionTypes.FUNCTION_RETI: case _:
print('RETI;') return 'unknown instruction'
case _:
print('unknown instruction')

48
index.html Normal file
View file

@ -0,0 +1,48 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"></meta>
<!-- TODO probably use something else that doesn't use Cloudflare -->
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/brython@3.12.2/brython.min.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/brython@3.12.2/brython_stdlib.js"></script>
<script type="text/python" src="./dis.py" id="unspdis"></script>
<title>unsp-dasm</title>
<script type="text/python">
import browser
import unspdis
dis_input = browser.document['disassemble-input']
dis_output = browser.document['disassemble-output']
def hello(ev):
args = []
for value in dis_input.value.split(' '):
args.append(int(value, 16))
if len(args) == 1:
args.append(0)
dis_output.value = unspdis.disassemble(args)
browser.document['disassemble-button'].bind('click', hello)
</script>
</head>
<body style="font-family: sans-serif;">
<h1>unsp-das</h1>
<noscript>You seem to have JavaScript disabled. If you don't want to enable it, feel free to download the program below.</noscript>
<div class="disassembler">
<textarea id="disassemble-input" cols="20" rows="3" placeholder="93FF"></textarea>
<textarea id="disassemble-output" cols="20" rows="3" disabled></textarea>
<button id="disassemble-button">Disassemble</button>
</div>
<div class="info">
<h2>What is this?</h2>
<p>This is an personal attempt at creating a disassembler for the &micro;'nSP ISA created by Sunplus Technology.</p>
<p>The &micro;'nSP architecture is an 16-bit CPU architecture that was used on microcontrollers and some plug-and-play video game devices (Such as the various Jakks Pacific GameKey-board consoles and the VTech V.Smile).</p>
<p>This disassembler was written in Python and runs on the web using <a href="https://brython.info">Brython</a>.</p>
<p>Source is available at <a href="https://git.nadeko.net/Serg2/unsp-dasm">git.nadeko.net/Serg2/unsp-dasm</a> and is licensed under the MIT No Attribution license.</p>
</div>
</body>
</html>