diff --git a/Makefile b/Makefile index 384a649855fe763498074f0668896835775a2473..deb828bf156c9c33b646a292f04679b3fe838d08 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -OBJS = ilxi.o alu.o asm.o cpu.o error.o host.o storage.o lexer.o +OBJS = ilxi.o alu.o asm.o cpu.o error.o host.o storage.o lexer.o inst.o FBCFLAGS = -g #-d LEXDEBUG @@ -26,6 +26,9 @@ storage.o: storage.bas lexer.o: lexer.bas fbc $(FBCFLAGS) -o lexer.o -c lexer.bas +inst.o: inst.bas + fbc $(FBCFLAGS) -o inst.o -c inst.bas + ilxi.o: ilxi.bas fbc -m ilxi $(FBCFLAGS) -o ilxi.o -c ilxi.bas diff --git a/asm.bas b/asm.bas index d159e02d08bdc293e93cf30e4011b7e471be6c2c..cff07304ebece78e338498c5adb08880c37a1e41 100644 --- a/asm.bas +++ b/asm.bas @@ -493,7 +493,7 @@ function asm_operand_count(opcode as ubyte) as ubyte select case opcode case OP_COPY, OP_ADD, OP_SUB, OP_MUL, OP_DIV, OP_SHL, OP_SHR, OP_OR, OP_NOT, OP_AND, OP_XOR, OP_EQV return 2 - case OP_CMP + case OP_CMP, OP_LCALL return 2 case OP_BRANCH, OP_SCALL, OP_ICALL, OP_PUSH, OP_POP return 1 @@ -512,7 +512,6 @@ function asm_disassemble(page as ushort, offset as ushort) as string dim operands() as t_operand dim operand_count as ubyte dim i as ushort - dim displacement as ushort dim ops_following as ubyte dim actual_amod as ubyte @@ -529,8 +528,6 @@ function asm_disassemble(page as ushort, offset as ushort) as string redim operands(operand_count) as t_operand - - for i = 1 to operand_count offset = offset + 1 @@ -541,8 +538,6 @@ function asm_disassemble(page as ushort, offset as ushort) as string actual_amod = asm_amod_amod(operands(i).amod) -'print "for operand "; i; ", got amod byte of "; actual_amod; " (binary "; bin(actual_amod); ")" - displacement = asm_decode_disp(asm_amod_disp(operands(i).amod)) select case actual_amod diff --git a/cpu.bas b/cpu.bas index 4be8a32dbdcdeb3b56ecd86aebf13c75be190e6a..5e8b0bff21b2aa495d991c9280a9dae7891704f5 100644 --- a/cpu.bas +++ b/cpu.bas @@ -6,6 +6,7 @@ #include "storage.bi" #include "asm.bi" #include "ilxi.bi" +#include "inst.bi" sub init_cpu() @@ -78,122 +79,138 @@ sub cpu() select case address_mode case AM_IMM - operands(i).low_byte = cpu_fetch() - operands(i).high_byte = cpu_fetch() - operands(i).byte_count = 2 - operands(i).immediate = 1 + with operands(i) + .low_byte = cpu_fetch() + .high_byte = cpu_fetch() + .byte_count = 2 + .immediate = 1 + end with case AM_REGD - operands(i).low_byte = cpu_fetch() - operands(i).byte_count = 1 - operands(i).displacement = displacement - operands(i).register = 1 - operands(i).has_displacement = 1 + with operands(i) + .low_byte = cpu_fetch() + .byte_count = 1 + .register = 1 + end with case AM_MEMD - operands(i).low_byte = cpu_fetch() - operands(i).high_byte = cpu_fetch() - operands(i).byte_count = 2 - operands(i).displacement = displacement - operands(i).memory = 1 - operands(i).has_displacement = 1 + with operands(i) + .low_byte = cpu_fetch() + .high_byte = cpu_fetch() + .byte_count = 2 + .memory = 1 + end with case AM_REGDD - operands(i).low_byte = cpu_fetch() - operands(i).byte_count = 1 - operands(i).displacement = displacement - operands(i).register = 1 - operands(i).has_displacement = 1 + with operands(i) + .low_byte = cpu_fetch() + .byte_count = 1 + .displacement = displacement + .register = 1 + .has_displacement = 1 + end with case AM_MEMDD - operands(i).low_byte = cpu_fetch() - operands(i).high_byte = cpu_fetch() - operands(i).byte_count = 2 - operands(i).displacement = displacement - operands(i).has_displacement = 1 - operands(i).memory = 1 + with operands(i) + .low_byte = cpu_fetch() + .high_byte = cpu_fetch() + .byte_count = 2 + .displacement = displacement + .has_displacement = 1 + .memory = 1 + end with case AM_REGI - operands(i).low_byte = cpu_fetch() - operands(i).byte_count = 1 - operands(i).register = 1 - operands(i).indirect = 1 + with operands(i) + .low_byte = cpu_fetch() + .byte_count = 1 + .register = 1 + .indirect = 1 + end with case AM_MEMI - operands(i).low_byte = cpu_fetch() - operands(i).high_byte = cpu_fetch() - operands(i).byte_count = 2 - operands(i).memory = 1 - operands(i).indirect = 1 + with operands(i) + .low_byte = cpu_fetch() + .high_byte = cpu_fetch() + .byte_count = 2 + .memory = 1 + .indirect = 1 + end with case AM_REGID - operands(i).low_byte = cpu_fetch() - operands(i).byte_count = 1 - operands(i).displacement = displacement - operands(i).register = 1 - operands(i).indirect = 1 - operands(i).has_displacement = 1 + with operands(i) + .low_byte = cpu_fetch() + .byte_count = 1 + .displacement = displacement + .register = 1 + .indirect = 1 + .has_displacement = 1 + end with case AM_MEMID - operands(i).low_byte = cpu_fetch() - operands(i).high_byte = cpu_fetch() - operands(i).byte_count = 2 - operands(i).displacement = displacement - operands(i).memory = 1 - operands(i).has_displacement = 1 - operands(i).indirect = 1 + with operands(i) + .low_byte = cpu_fetch() + .high_byte = cpu_fetch() + .byte_count = 2 + .displacement = displacement + .memory = 1 + .has_displacement = 1 + .indirect = 1 + end with end select next i select case opcode case OP_COPY - + inst_copy operands(1), operands(2) case OP_ADD - + inst_add operands(1), operands(2) case OP_SUB - + inst_sub operands(1), operands(2) case OP_MUL - + inst_mul operands(1), operands(2) case OP_DIV - + inst_div operands(1), operands(2) case OP_SHL - + inst_shl operands(1), operands(2) case OP_SHR - - case OP_OR - + inst_shr operands(1), operands(2) + case OP_OR + inst_or operands(1), operands(2) case OP_NOT - + inst_not operands(1), operands(2) case OP_AND - + inst_and operands(1), operands(2) case OP_XOR - + inst_xor operands(1), operands(2) case OP_EQV - + inst_eqv operands(1), operands(2) case OP_CMP - + inst_cmp operands(1), operands(2) case OP_BRANCH - + inst_branch operands(1) case OP_BEQ - + inst_beq operands(1) case OP_BNE - + inst_bne operands(1) case OP_BLT - + inst_blt operands(1) case OP_BGT - + inst_bgt operands(1) + case OP_BZ + inst_bz operands(1) case OP_SCALL - + inst_scall operands(1) case OP_LCALL - + inst_lcall operands(1), operands(2) case OP_ICALL - + inst_icall operands(1) case OP_SRET - + inst_sret case OP_LRET - + inst_lret case OP_IRET - + inst_iret case OP_PUSH - + inst_push operands(1) case OP_POP - + inst_pop operands(1) case OP_NOP - + ' do nothing case OP_HLT - cpu_set_flag FL_HALT + inst_hlt case else end select @@ -221,7 +238,7 @@ sub cpu() loop -end sub +end sub ' cpu() function cpu_fetch() as ubyte dim t_byte as ubyte = 0 @@ -230,7 +247,11 @@ function cpu_fetch() as ubyte cpu_state.pc += 1 return t_byte -end function +end function ' cpu_fetch() + +function cpu_get_effective_address(operand as t_operand) as ushort + +end function ' cpu_get_effective_address() sub cpu_dump_state() dim x as t_cpu_state @@ -255,17 +276,18 @@ sub cpu_dump_state() print " LF="; cpu_get_flag(FL_LESSTHAN); " GF="; cpu_get_flag(FL_GREATERTHAN); " ZF="; cpu_get_flag(FL_ZERO); print " PL=0 PF="; cpu_get_flag(FL_PARITY); " SF="; cpu_get_flag(FL_SIGN); " DF="; cpu_get_flag(FL_DEBUG); print "" -end sub + +end sub ' cpu_dump_state() sub cpu_set_flag(flag as ushort) cpu_state.fl = cpu_state.fl or flag -end sub +end sub ' cpu_set_flag() sub cpu_clear_flag(flag as ushort) if cpu_get_flag(flag) = 1 then cpu_state.fl = (not cpu_state.fl) and flag end if -end sub +end sub ' cpu_clear_flag() function cpu_get_flag(flag as ushort) as ubyte if (cpu_state.fl and flag) = flag then @@ -273,15 +295,15 @@ function cpu_get_flag(flag as ushort) as ubyte else return 0 end if -end function +end function ' cpu_get_flag() function cpu_get_pl() as ubyte return (cpu_state.fl and PL_MASK) shr 9 -end function +end function ' cpu_get_pl() sub cpu_set_pl(privilege_level as ubyte) cpu_state.fl or= (privilege_level shl 9) -end sub +end sub ' cpu_set_pl() sub cpu_set_reg_alpha(register as string, value as ushort) @@ -337,7 +359,7 @@ sub cpu_set_reg_alpha(register as string, value as ushort) case REG_GP cpu_state.gp = value end select -end sub +end sub ' cpu_set_reg_alpha() function cpu_get_reg_alpha(register as string) as ushort @@ -394,4 +416,4 @@ function cpu_get_reg_alpha(register as string) as ushort return cpu_state.gp end select -end function \ No newline at end of file +end function ' cpu_get_reg_alpha() \ No newline at end of file diff --git a/inst.bas b/inst.bas new file mode 100644 index 0000000000000000000000000000000000000000..a2e8f064eeb12e5f332d8ccf61011f670dfa64af --- /dev/null +++ b/inst.bas @@ -0,0 +1,121 @@ +' +' inst.bas +' + +#include "inst.bi" +#include "storage.bi" +#include once "asm.bi" +#include "cpu.bi" +#include "ilxi.bi" + +sub inst_copy(dest as t_operand, source as t_operand) + +end sub ' inst_copy() + +sub inst_add(dest as t_operand, source as t_operand) + +end sub ' inst_add() + +sub inst_sub(dest as t_operand, source as t_operand) + +end sub ' inst_sub() + +sub inst_mul(dest as t_operand, source as t_operand) + +end sub ' inst_mul() + +sub inst_div(dest as t_operand, source as t_operand) + +end sub ' inst_div() + +sub inst_shl(dest as t_operand, count as t_operand) + +end sub ' inst_shl() + +sub inst_shr(dest as t_operand, count as t_operand) + +end sub ' inst_shr() + +sub inst_or(dest as t_operand, source as t_operand) + +end sub ' inst_or() + +sub inst_not(dest as t_operand, source as t_operand) + +end sub ' inst_not() + +sub inst_and(dest as t_operand, source as t_operand) + +end sub ' inst_and() + +sub inst_xor(dest as t_operand, source as t_operand) + +end sub ' inst_xor() + +sub inst_eqv(dest as t_operand, source as t_operand) + +end sub ' inst_eqv() + +sub inst_cmp(dest as t_operand, source as t_operand) + +end sub ' inst_cmp() + +sub inst_branch(dest as t_operand) + +end sub ' inst_branch() + +sub inst_beq(dest as t_operand) + +end sub ' inst_beq() + +sub inst_bne(dest as t_operand) + +end sub ' inst_bne() + +sub inst_blt(dest as t_operand) + +end sub ' inst_blt() + +sub inst_bgt(dest as t_operand) + +end sub ' inst_bgt() + +sub inst_bz(dest as t_operand) + +end sub ' inst_bz() + +sub inst_scall(dest as t_operand) + +end sub ' inst_scall() + +sub inst_lcall(dest as t_operand, page as t_operand) + +end sub ' inst_lcall() + +sub inst_icall(dest as t_operand) + +end sub ' inst_icall() + +sub inst_sret() + +end sub ' inst_sret() + +sub inst_lret() + +end sub ' inst_lret() + +sub inst_iret() + +end sub ' inst_iret() + +sub inst_push(dest as t_operand) + +end sub ' inst_push() + +sub inst_pop(dest as t_operand) + +end sub ' inst_pop() + +sub inst_hlt() + cpu_set_flag FL_HALT +end sub ' inst_hlt() \ No newline at end of file diff --git a/inst.bi b/inst.bi new file mode 100644 index 0000000000000000000000000000000000000000..dc0c0c4028b66905816eae3a768d5963224d79f6 --- /dev/null +++ b/inst.bi @@ -0,0 +1,34 @@ +' +' inst.bi +' + +#include once "asm.bi" + +declare sub inst_copy(dest as t_operand, source as t_operand) +declare sub inst_add(dest as t_operand, source as t_operand) +declare sub inst_sub(dest as t_operand, source as t_operand) +declare sub inst_mul(dest as t_operand, source as t_operand) +declare sub inst_div(dest as t_operand, source as t_operand) +declare sub inst_shl(dest as t_operand, count as t_operand) +declare sub inst_shr(dest as t_operand, count as t_operand) +declare sub inst_or(dest as t_operand, source as t_operand) +declare sub inst_not(dest as t_operand, source as t_operand) +declare sub inst_and(dest as t_operand, source as t_operand) +declare sub inst_xor(dest as t_operand, source as t_operand) +declare sub inst_eqv(dest as t_operand, source as t_operand) +declare sub inst_cmp(dest as t_operand, source as t_operand) +declare sub inst_branch(dest as t_operand) +declare sub inst_beq(dest as t_operand) +declare sub inst_bne(dest as t_operand) +declare sub inst_blt(dest as t_operand) +declare sub inst_bgt(dest as t_operand) +declare sub inst_bz(dest as t_operand) +declare sub inst_scall(dest as t_operand) +declare sub inst_lcall(dest as t_operand, page as t_operand) +declare sub inst_icall(dest as t_operand) +declare sub inst_sret() +declare sub inst_lret() +declare sub inst_iret() +declare sub inst_push(dest as t_operand) +declare sub inst_pop(dest as t_operand) +declare sub inst_hlt() \ No newline at end of file