riscv_emulator/src/vm/assembler.v

68 lines
1.4 KiB
V

import kernel as os
__global mneumonics = {
// Menumonic : [TYPE | ( OPCODE, FUNC3, (FUNC7 | IMM) )]
'ADD' : [0x33, 0x0],
'SUB' : [0x33, 0x0],
'XOR' : [0x33, 0x4],
'OR' : [0x33, 0x6],
'AND' : [0x33, 0x7],
'SLL' : [0x33, 0x1],
'SRL' : [0x33, 0x5],
'SRA' : [0x33, 0x5],
'SLT' : [0x33, 0x2],
'SLTU' : [0x33, 0x3],
'ADDI' : [0x13, 0x0],
'XORI' : [0x13, 0x4],
'ORI' : [0x33, 0x6],
'ANDI' : [0x13, 0x7],
'SLLI' : [0x13, 0x1],
'SRLI' : [0x33, 0x5],
'SRAI' : [0x13, 0x5],
'SLTI' : [0x13, 0x2],
'SLTIU' : [0x13, 0x3],
'LB' : [0x03, 0x0],
'LH' : [0x03, 0x1],
'LW' : [0x03, 0x2],
'LBU' : [0x03, 0x4],
'LHU' : [0x03, 0x5],
'SB' : [0x23, 0x0],
'SH' : [0x23, 0x1],
'SW' : [0x23, 0x2],
'BEQ' : [0x63, 0x0],
'BNE' : [0x63, 0x1],
'BLT' : [0x63, 0x4],
'BGE' : [0x63, 0x5],
'BLTU' : [0x63, 0x6],
'BGEU' : [0x63, 0x7],
'JAL' : [0x6F, 0x0],
'JALR' : [0x67, 0x0],
'LUI' : [0x37],
'AUIPC' : [0x17],
'ECALL' : [0x73, 0x0, 0x0],
'EBREAK': [0x73, 0x0, 0x1],
}
__global registers = {
}
fn bytecode(asm_buffer [u8])
{
mut asm_instruction := asm_buffer.pop()
mut mneumonic := asm_instruction.split(' ')[0].to_upper()
bytecodes := mneumonics[mneumonic]
mut opcode := bytecodes[0]
mut func3 := bytecodes[1]
mut func7_or_imm := bytecodes[2]
}
fn start_assembler()
{
assert os.kernel != none
for{ if kernel.stdin.len != 0 { bytecode(&kernel.stdin) } }
}