From 303f5e06a7fdc25af999eac8d9ccfd8b7376b4e9 Mon Sep 17 00:00:00 2001 From: Jaka Furlan Date: Sun, 30 Nov 2025 17:07:49 +0100 Subject: [PATCH] koncal sicxe --- ass2/SICocaml/sicxeDune/bin/dune | 2 +- ass2/SICocaml/sicxeDune/bin/main.ml | 5 ++-- ass2/SICocaml/sicxeDune/lib/dune | 2 +- ass2/SICocaml/sicxeDune/lib/izvajalnik.ml | 36 +++++++++++++---------- ass2/SICocaml/sicxeDune/lib/pogajalnik.ml | 2 +- 5 files changed, 27 insertions(+), 20 deletions(-) diff --git a/ass2/SICocaml/sicxeDune/bin/dune b/ass2/SICocaml/sicxeDune/bin/dune index a16b16a..6c56e90 100644 --- a/ass2/SICocaml/sicxeDune/bin/dune +++ b/ass2/SICocaml/sicxeDune/bin/dune @@ -1,4 +1,4 @@ (executable (public_name sicxeDune) (name main) - (libraries sicxeDune)) + (libraries sicxeDune notty notty.unix)) diff --git a/ass2/SICocaml/sicxeDune/bin/main.ml b/ass2/SICocaml/sicxeDune/bin/main.ml index e6e4c5e..c5f24d6 100644 --- a/ass2/SICocaml/sicxeDune/bin/main.ml +++ b/ass2/SICocaml/sicxeDune/bin/main.ml @@ -12,8 +12,9 @@ let test_runner () = Loader.load_object_file state "/mnt/c/Programiranje/SPO/ass1/horner.obj"; Processor.print_memory state 50; Printf.printf "\n\n---Starting execution!---\n\n"; - Pogajalnik.run state khz + Pogajalnik.run state khz (* Run the test *) -let () = test_runner () \ No newline at end of file +let () = test_runner () + diff --git a/ass2/SICocaml/sicxeDune/lib/dune b/ass2/SICocaml/sicxeDune/lib/dune index 039bb16..472254b 100644 --- a/ass2/SICocaml/sicxeDune/lib/dune +++ b/ass2/SICocaml/sicxeDune/lib/dune @@ -1,4 +1,4 @@ (library (name sicxeDune) (wrapped false) - (libraries unix)) + (libraries unix notty notty.unix)) diff --git a/ass2/SICocaml/sicxeDune/lib/izvajalnik.ml b/ass2/SICocaml/sicxeDune/lib/izvajalnik.ml index afef4c1..288a6f8 100644 --- a/ass2/SICocaml/sicxeDune/lib/izvajalnik.ml +++ b/ass2/SICocaml/sicxeDune/lib/izvajalnik.ml @@ -21,8 +21,14 @@ let notImplemented (mnemonic : string) = Printf.printf "mnemonic %s is not imple let invalidOpcode (opcode : int) = Printf.printf "opcode %d is invalid!\n" opcode let invalidAdressing () = Printf.printf "invalid adressing!\n" - - +(*spremeni disp v signed če se uporablja PC-relativno*) +let to_signed (x : int) : int = + let mask12 = 0xFFF in (* keep only 12 bits *) + let x = x land mask12 in (* mask input to 12 bits *) + if x land 0x800 <> 0 then (* if the 24th bit (sign bit) is set *) + x - (1 lsl 12) (* subtract 2^24 to get negative value *) + else + x (*beri drugi byte ukaza formata 2 in vrni r1 in r2, kot int njunih vrednosti (reg a -> 1, reg x -> 2 ...)*) let readR1R2 (state : Processor.state) : (int * int) = @@ -67,11 +73,11 @@ let readAddress (state : Processor.state) : int = let address = (addr_high lsl 8) lor byte4 in address -(*pridobi operand*) -let getOperandF3 (state : Processor.state) (nixbpe : nixbpe) (disp : int) : int = +(*pridobi effective address in operand*) +let getOperandF3 (state : Processor.state) (nixbpe : nixbpe) (disp : int) : int * int= let ea = if nixbpe.b = 1 && nixbpe.p = 0 then state.regs.b + disp (*B relativno*) - else if nixbpe.p = 1 && nixbpe.b = 0 then state.regs.pc + disp + 3(*PC relativno, ker PC povečamo šele po klicu te funkcije moramo tu +3*) + else if nixbpe.p = 1 && nixbpe.b = 0 then state.regs.pc + to_signed disp + 3(*PC relativno, ker PC povečamo šele po klicu te funkcije moramo tu +3*) else disp (*direktno*) in let ea = if nixbpe.x = 1 then ea + state.regs.x else ea in (*+ (X) po potrebi*) @@ -83,9 +89,9 @@ let getOperandF3 (state : Processor.state) (nixbpe : nixbpe) (disp : int) : int else if nixbpe.n = 1 && nixbpe.i = 0 then Processor.readMemAddr state (Processor.readMemAddr state ea) (* indirect *) else failwith "Invalid addressing mode" in - value + ea ,value -let getOperandF4 (state : Processor.state) (nixbpe : nixbpe) (disp : int) : int = +let getOperandF4 (state : Processor.state) (nixbpe : nixbpe) (disp : int) : int * int = let ea = if nixbpe.b = 1 && nixbpe.p = 0 then state.regs.b + disp (*B relativno*) else if nixbpe.p = 1 && nixbpe.b = 0 then state.regs.pc + disp + 4(*PC relativno, ker PC povečamo šele po klicu te funkcije moramo tu +4*) @@ -100,7 +106,7 @@ let getOperandF4 (state : Processor.state) (nixbpe : nixbpe) (disp : int) : int else if nixbpe.n = 1 && nixbpe.i = 0 then Processor.readMemAddr state (Processor.readMemAddr state ea) (* indirect *) else failwith "Invalid addressing mode" in - value + ea, value (*execute format 1*) let executeFormat1 (state : Processor.state) (mnemonic : OpcodeTable.mnemonic) : unit = @@ -147,7 +153,7 @@ let executeFormat2 (state: Processor.state) (mnemonic : OpcodeTable.mnemonic) : (*execute Format 3*) let executeFormat3 (state : Processor.state) (nixbpe : nixbpe) (mnemonic: OpcodeTable.mnemonic): unit = let disp = readDisp state in - let operand = getOperandF3 state nixbpe disp in + let ea ,operand = getOperandF3 state nixbpe disp in (*debugging*) Printf.printf "[Izvajalnik/executeFormat3] Mnemonic: %s, nixbpe: %s, operand: %d = 0x%02X\n" (string_of_mnemonic mnemonic) (string_of_nixbpe nixbpe) operand operand; @@ -168,11 +174,11 @@ let executeFormat3 (state : Processor.state) (nixbpe : nixbpe) (mnemonic: Opcode | WD -> notImplemented "WD3" (* Jump / subroutine *) - | J -> IzvajalnikF3.j state operand - | JEQ -> IzvajalnikF3.jeq state operand - | JGT -> IzvajalnikF3.jgt state operand - | JLT -> IzvajalnikF3.jlt state operand - | JSUB -> IzvajalnikF3.jsub state operand + | J -> IzvajalnikF3.j state ea + | JEQ -> IzvajalnikF3.jeq state ea + | JGT -> IzvajalnikF3.jgt state ea + | JLT -> IzvajalnikF3.jlt state ea + | JSUB -> IzvajalnikF3.jsub state ea | RSUB -> IzvajalnikF3.rsub state (* Load/store *) @@ -202,7 +208,7 @@ let executeFormat3 (state : Processor.state) (nixbpe : nixbpe) (mnemonic: Opcode let executeFormat4 (state : Processor.state) (nixbpe : nixbpe) (mnemonic: OpcodeTable.mnemonic): unit = let address = readAddress state in - let operand = getOperandF4 state nixbpe address in + let _, operand = getOperandF4 state nixbpe address in (*debugging*) Printf.printf "[Izvajalnik/executeFormat4] Mnemonic: %s, nixbpe: %s, operand: %d = 0x%02X\n" (string_of_mnemonic mnemonic) (string_of_nixbpe nixbpe) operand operand; diff --git a/ass2/SICocaml/sicxeDune/lib/pogajalnik.ml b/ass2/SICocaml/sicxeDune/lib/pogajalnik.ml index d7c493d..9f42d7d 100644 --- a/ass2/SICocaml/sicxeDune/lib/pogajalnik.ml +++ b/ass2/SICocaml/sicxeDune/lib/pogajalnik.ml @@ -27,4 +27,4 @@ let run (state : Processor.state) (khz : float) : unit = Printf.printf "PC stuck at 0x%X 5 times. Ending loop.\n" current_pc else loop state last_pc count - in loop state state.regs.pc 0 \ No newline at end of file + in loop state state.regs.pc 0