;*********************************************************** ;tiny emulator...in tiny ;author: Tim Wylie ;why?: if you have to ask, you'll never understand anyway. ;*********************************************************** b Main ;*****************Get_Program************************ Get_Program: ;pop ;st Get_Program_retaddr lda Get_Program_InputSize jsb printc jsb Inputn st MAXSIZE lda Get_Program_Inst jsb printc lda Get_Program_endl jsb printc ld Get_Program_#0 st Get_Program_Counter Get_Program_Do_1: lda memory add Get_Program_Counter st Get_Program_Memory_Index ld Get_Program_Counter jsb printn lda Get_Program_Colon jsb printc jsb inputn sti Get_Program_Memory_Index ld Get_Program_Counter add Get_Program_#1 st Get_Program_Counter Get_Program_While_1: ld Get_Program_Counter sub MAXSIZE bz Get_Program_End_Do_While_1 b Get_Program_Do_1 Get_Program_End_Do_While_1: rsb ;Get_Program constants Get_Program_Inst: dc 'Enter Machine Code: ' db 0 Get_Program_InputSize: dc 'Enter Size of Program(Max is 350): ' db 0 Get_Program_endl: db 13 db 10 db 0 Get_Program_Colon: dc ':' db 0 Get_Program_#0: db 0 Get_Program_#1: db 1 ;getprogram variables Get_Program_Counter: db 0 Get_Program_Memory_Index: db 0 ;Get_Program_Zeros: db 0 ;Get_Program_retaddr: ds 1 ;*************************GetOpcode********************** GetOpcode: pop st GetOpcode_retaddr pop st GetOpcode_Memory ld GetOpcode_retaddr push ld GetOpcode_Memory div GetOpcode_#1000 st GetOpcode_Memory ld GetOpcode_Memory rsb ;GetOpcode variables GetOpcode_retaddr: ds 1 GetOpcode_Memory: ds 1 ;GetOpcode constants GetOpcode_#1000: db 1000 ;*************************************************************** ;************************GetOperand************************ GetOperand: pop st GetOperand_retaddr pop st GetOperand_Memory ld GetOperand_retaddr push GetOperand_While_1: ld GetOperand_Memory sub GetOperand_#1000 bn GetOperand_While_End_1 GetOperand_While_Begin_1: ld GetOperand_Memory sub GetOperand_#1000 st GetOperand_Memory b GetOperand_While_1 GetOperand_While_End_1: ld GetOperand_Memory rsb ;Initialize constants GetOperand_#1000: db 1000 ;Initialize variables GetOperand_Memory: db 0 GetOperand_retaddr: ds 1 ;************************************************************ Main: ld #0 st program_counter jsb Get_Program ld MAXSIZE st stack_pointer lda Start jsb printc lda endl jsb printc While_1: ld CPUIsRunning sub #1 bz While_Begin_1 b While_End_1 While_Begin_1: lda memory add program_counter st Memory_Index ldi Memory_Index push jsb GetOpcode st opcode ldi Memory_Index push jsb GetOperand st operand ;stop If_0: ld opcode sub #0 bz Then_0 b Else_0 Then_0: ld #0 st CPUIsRunning b End_If_0 Else_0: ;ld x If_1: ld opcode sub #1 bz Then_1 b Else_1 Then_1: lda memory add operand st Temp ldi Temp st ACC b End_If_1 Else_1: ;ldi x If_2: ld opcode sub #2 bz Then_2 b Else_2 Then_2: lda memory add operand st Temp ldi Temp st Temp ldi Temp st ACC b End_If_2 Else_2: ;lda x If_3: ld opcode sub #3 bz Then_3 b Else_3 Then_3: ld operand st ACC b End_If_3 Else_3: ;st x If_4: ld opcode sub #4 bz Then_4 b Else_4 Then_4: lda memory add operand st Temp ld ACC sti Temp b End_If_4 Else_4: ;sti x If_5: ld opcode sub #5 bz Then_5 b Else_5 Then_5: lda memory add operand st Temp ldi Temp st Temp ld ACC sti Temp b End_If_5 Else_5: ;add x If_6: ld opcode sub #6 bz Then_6 b Else_6 Then_6: lda memory add operand st Temp ldi Temp add ACC st ACC b End_If_6 Else_6: ;sub x If_7: ld opcode sub #7 bz Then_7 b Else_7 Then_7: lda memory add operand st Temp ldi Temp st Temp ld ACC sub Temp st ACC b End_If_7 Else_7: ;mul x If_8: ld opcode sub #8 bz Then_8 b Else_8 Then_8: lda memory add operand st Temp ldi Temp mul ACC st ACC b End_If_8 Else_8: ;div x If_9: ld opcode sub #9 bz Then_9 b Else_9 Then_9: lda memory add operand st Temp ldi Temp st Temp ld ACC div Temp st ACC b End_If_9 Else_9: ;in If_10: ld opcode sub #10 bz Then_10 b Else_10 Then_10: in st ACC b End_If_10 Else_10: ;out If_11: ld opcode sub #11 bz Then_11 b Else_11 Then_11: ld ACC out b End_If_11 Else_11: ;b x If_12: ld opcode sub #12 bz Then_12 b Else_12 Then_12: ld operand sub #1 st program_counter b End_If_12 Else_12: ;bp x If_13: ld opcode sub #13 bz Then_13 b Else_13 Then_13: If_13_1: ld ACC bp Then_13_1 b Else_13_1 Then_13_1: ld operand sub #1 st program_counter b End_If_13_1 Else_13_1: End_If_13_1: b End_If_13 Else_13: ;bn x If_14: ld opcode sub #14 bz Then_14 b Else_14 Then_14: If_14_1: ld ACC bn Then_14_1 b Else_14_1 Then_14_1: ld operand sub #1 st program_counter b End_If_14_1 Else_14_1: End_If_14_1: b End_If_14 Else_14: ;bz x If_15: ld opcode sub #15 bz Then_15 b Else_15 Then_15: If_15_1: ld ACC bz Then_15_1 b Else_15_1 Then_15_1: ld operand sub #1 st program_counter b End_If_15_1 Else_15_1: End_If_15_1: b End_If_15 Else_15: ;jsb x If_16: ld opcode sub #16 bz Then_16 b Else_16 Then_16: ld stack_pointer sub #1 st stack_pointer lda memory add stack_pointer st Temp ld program_counter sti Temp ld operand st program_counter ;do work for 4 functions here If_16_900: ld operand sub #900 bz Then_16_900 b Else_16_900 Then_16_900: ld ACC jsb printn lda memory add stack_pointer st Temp ldi Temp st program_counter ld stack_pointer add #1 st stack_pointer b End_If_16_900 Else_16_900: If_16_925: ld operand sub #925 bz Then_16_925 b Else_16_925 Then_16_925: lda memory add ACC jsb printc lda memory add stack_pointer st Temp ldi Temp st program_counter ld stack_pointer add #1 st stack_pointer b End_If_16_925 Else_16_925: If_16_950: ld operand sub #950 bz Then_16_950 b Else_16_950 Then_16_950: jsb inputn st ACC lda memory add stack_pointer st Temp ldi Temp st program_counter ld stack_pointer add #1 st stack_pointer b End_If_16_950 Else_16_950: If_16_975: ld operand sub #975 bz Then_16_975 b Else_16_975 Then_16_975: lda memory add ACC jsb inputc lda memory add stack_pointer st Temp ldi Temp st program_counter ld stack_pointer add #1 st stack_pointer b End_If_16_975 Else_16_975: ld program_counter sub #1 st program_counter End_If_16_975: End_If_16_950: End_If_16_925: End_If_16_900: b End_If_16 Else_16: ;rsb x If_17: ld opcode sub #17 bz Then_17 b Else_17 Then_17: lda memory add stack_pointer st Temp ldi Temp st program_counter ld stack_pointer add #1 st stack_pointer b End_If_17 Else_17: ;push If_18: ld opcode sub #18 bz Then_18 b Else_18 Then_18: ld stack_pointer sub #1 st stack_pointer lda memory add stack_pointer st Temp ld ACC sti Temp b End_If_18 Else_18: ;pop If_19: ld opcode sub #19 bz Then_19 b Else_19 Then_19: lda memory add stack_pointer st Temp ldi Temp st ACC ld stack_pointer add #1 st stack_pointer b End_If_19 Else_19: ;no other opcodes lda BadOpcode jsb printc ld #0 st CPUIsRunning End_If_19: End_If_18: End_If_17: End_If_16: End_If_15: End_If_14: End_If_13: End_If_12: End_If_11: End_If_10: End_If_9: End_If_8: End_If_7: End_If_6: End_If_5: End_If_4: End_If_3: End_If_2: End_If_1: End_If_0: ld program_counter add #1 st program_counter b While_1 While_End_1: lda endl jsb printc lda Stopped jsb printc stop ;global constants #0: db 0 #1: db 1 #2: db 2 #3: db 3 #4: db 4 #5: db 5 #6: db 6 #7: db 7 #8: db 8 #9: db 9 #10: db 10 #11: db 11 #12: db 12 #13: db 13 #14: db 14 #15: db 15 #16: db 16 #17: db 17 #18: db 18 #19: db 19 Start: dc 'Beginning Emulation:' db 0 endl: db 13 db 10 db 0 #900: db 900 #925: db 925 #950: db 950 #975: db 975 Stopped: dc 'Emulation Done' db 0 BadOpcode: dc 'Error: Illegal Opcode' db 0 ;global variables ACC: db 0 program_counter: db 0 stack_pointer: db 0 ;stackpointer needs to point to memory+MAXSIZE CPUIsRunning: db 1 Memory_Index: ds 1 Temp: ds 1 MAXSIZE: db 400 ;how big the arrays are opcode: ds 1 operand: ds 1 memory: ds 350