started work on ass2
This commit is contained in:
parent
b16c3e23d8
commit
1b990e190c
16 changed files with 360 additions and 0 deletions
BIN
ass2/SICocaml/decoder.cmi
Normal file
BIN
ass2/SICocaml/decoder.cmi
Normal file
Binary file not shown.
BIN
ass2/SICocaml/decoder.cmo
Normal file
BIN
ass2/SICocaml/decoder.cmo
Normal file
Binary file not shown.
BIN
ass2/SICocaml/decoder.cmx
Normal file
BIN
ass2/SICocaml/decoder.cmx
Normal file
Binary file not shown.
10
ass2/SICocaml/decoder.ml
Normal file
10
ass2/SICocaml/decoder.ml
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
(*decoderjeva naloga je pridobiti mnemonic in tip ukaza iz prvega byta z pogledom v hash-table*)
|
||||
let table = OpcodeTable.table
|
||||
|
||||
let decoder (byte1 : char) : OpcodeTable.info =
|
||||
let opcode = (Char.code byte1) land 0xFC in
|
||||
try
|
||||
Hashtbl.find OpcodeTable.table opcode
|
||||
with
|
||||
| Not_found -> failwith "invalid opcode"
|
||||
|
||||
BIN
ass2/SICocaml/decoder.o
Normal file
BIN
ass2/SICocaml/decoder.o
Normal file
Binary file not shown.
BIN
ass2/SICocaml/izvajalnik.cmi
Normal file
BIN
ass2/SICocaml/izvajalnik.cmi
Normal file
Binary file not shown.
BIN
ass2/SICocaml/izvajalnik.cmo
Normal file
BIN
ass2/SICocaml/izvajalnik.cmo
Normal file
Binary file not shown.
124
ass2/SICocaml/izvajalnik.ml
Normal file
124
ass2/SICocaml/izvajalnik.ml
Normal file
|
|
@ -0,0 +1,124 @@
|
|||
type registers = {
|
||||
mutable a : int;
|
||||
mutable x : int;
|
||||
mutable l : int;
|
||||
mutable b : int;
|
||||
mutable s : int;
|
||||
mutable t : int;
|
||||
mutable f : int;
|
||||
mutable pc : int;
|
||||
mutable sw : int
|
||||
}
|
||||
(*tip state predstavlja stanje SIC/XE, z pomnilnikom in registri*)
|
||||
type state = {
|
||||
regs : registers;
|
||||
memory : Bytes.t
|
||||
}
|
||||
|
||||
type nixbpe = {
|
||||
n : int;
|
||||
i : int;
|
||||
x : int;
|
||||
b : int;
|
||||
p : int;
|
||||
e : int
|
||||
}
|
||||
|
||||
(*kreiramo začetno stanje*)
|
||||
let regs = {a = 0; x = 0; l = 0; b = 0; s = 0; t = 0; f = 0; pc = 0; sw = 0}
|
||||
let memSize = 1 lsl 20 (*2^20*)
|
||||
let memory = Bytes.make memSize '\x00' (*mutbale kos pomnilnika velikosti memSize*)
|
||||
let state = {regs; memory}
|
||||
|
||||
|
||||
(*----Funkcije izvajalnika----*)
|
||||
(*TODO - brali bomo vedno relativno od začetka instrukcije, PC bomo na koncu ukaza povečali za pravo velikost!*)
|
||||
|
||||
(*funkcije za javljanje napak*)
|
||||
let notImplemented (mnemonic : string) = Printf.printf "mnemonic %s is not implemented!\n" mnemonic
|
||||
let invalidOpcode (opcode : int) = Printf.printf "opcode %d is invalid!\n" opcode
|
||||
let invalidAdressing () = Printf.printf "invalid adressing!\n"
|
||||
|
||||
(*beri in povisaj PC*)
|
||||
let fetch (state : state) : char =
|
||||
let byte = Bytes.get state.memory state.regs.pc in
|
||||
state.regs.pc <- state.regs.pc + 1;
|
||||
byte
|
||||
|
||||
(*beri za offset in ne povecaj PC*)
|
||||
let readMem (state : state) (offset : int) : char =
|
||||
Bytes.get state.memory (state.regs.pc + offset)
|
||||
|
||||
|
||||
(*beri drugi byte ukaza formata 2 in vrni r1 in r2, kot int njunih vrednosti (reg a -> 1, reg x -> 2 ...)*)
|
||||
let readFormat2Byte2 (state : state) : (int * int) =
|
||||
let byte2 = Char.code (readMem state 1) in
|
||||
let highNibble = (byte2 land 0xF0) lsr 4 in (*pridobi prvi nibble*)
|
||||
let lowNibble = byte2 land 0x0F in (*pridobi drugi nibble*)
|
||||
(highNibble, lowNibble)
|
||||
|
||||
(*preberi byta 1 in 2 in dobi nixbpe bite*)
|
||||
let getNIXBPE (state : state) : nixbpe =
|
||||
let byte1 = Char.code (readMem state 0) in
|
||||
let byte2 = Char.code (readMem state 1) in
|
||||
{n = (byte1 lsr 1) land 1;
|
||||
i = byte1 land 1;
|
||||
x = (byte2 lsr 7) land 1;
|
||||
b = (byte2 lsr 6) land 1;
|
||||
p = (byte2 lsr 5) land 1;
|
||||
e = (byte2 lsr 4) land 1;}
|
||||
|
||||
|
||||
(*execute format 1*)
|
||||
let executeFormat1 (state : state) (mnemonic : OpcodeTable.mnemonic) : unit =
|
||||
match mnemonic with
|
||||
| FIX -> notImplemented "FIX"
|
||||
| FLOAT -> notImplemented "FLOAT"
|
||||
| HIO -> notImplemented "HIO"
|
||||
| NORM -> notImplemented "NORM"
|
||||
| SIO -> notImplemented "SIO"
|
||||
| TIO -> notImplemented "TIO"
|
||||
| _ -> Printf.printf "Mnemonic %s falsely flaged as format 1" (OpcodeTable.string_of_mnemonic mnemonic)
|
||||
|
||||
|
||||
(*execute format 2*)
|
||||
let executeFormat2 (state: state) (mnemonic : OpcodeTable.mnemonic) : unit =
|
||||
let (r1, r2) = readFormat2Byte2 state in
|
||||
match mnemonic with
|
||||
| ADDR -> notImplemented "ADD" (*when implemented: -> add state r1 r2*)
|
||||
| CLEAR -> notImplemented "CLEAR"
|
||||
| COMPR -> notImplemented "F2"
|
||||
| DIVR -> notImplemented "F2"
|
||||
| MULR -> notImplemented "F2"
|
||||
| RMO -> notImplemented "F2"
|
||||
| SHIFTL -> notImplemented "F2"
|
||||
| SHIFTR -> notImplemented "F2"
|
||||
| SUBR -> notImplemented "F2"
|
||||
| SVC -> notImplemented "F2"
|
||||
| TIXR -> notImplemented "F2"
|
||||
|_ -> Printf.printf "Mnemonic %s falsely flaged as format 1" (OpcodeTable.string_of_mnemonic mnemonic)
|
||||
|
||||
|
||||
let executeFormat3 (state : state) (nixbpe : nixbpe) : unit
|
||||
|
||||
|
||||
(*execute format 3*)
|
||||
let executeFormat3_4 (state : state) (mnemonic : OpcodeTable.mnemonic) : unit =
|
||||
let nixbpe = getNIXBPE state in
|
||||
match nixbpe.e with
|
||||
| 0 -> executeFormat3 state
|
||||
| 1 -> executeFormat4 state
|
||||
| _ -> failwith "invalid computation of nxbpe"
|
||||
|
||||
|
||||
(*execute ukaza*)
|
||||
let execute (state : state) : unit =
|
||||
let byte1 = readMem state 0 in (*read the 1st byte of the instruction*)
|
||||
try
|
||||
let {OpcodeTable.mnemonic; OpcodeTable.format} = Decoder.decoder byte1 in (*determen the format of the instruction from the 1st byte*)
|
||||
match format with
|
||||
| F1 -> executeFormat1 state mnemonic
|
||||
| F2 -> executeFormat2 state mnemonic
|
||||
| F3_4 -> executeFormat3_4 state mnemonic
|
||||
with
|
||||
| Failure msg -> byte1 |> Char.code |> invalidOpcode
|
||||
BIN
ass2/SICocaml/opcodeTable.cmi
Normal file
BIN
ass2/SICocaml/opcodeTable.cmi
Normal file
Binary file not shown.
BIN
ass2/SICocaml/opcodeTable.cmo
Normal file
BIN
ass2/SICocaml/opcodeTable.cmo
Normal file
Binary file not shown.
BIN
ass2/SICocaml/opcodeTable.cmx
Normal file
BIN
ass2/SICocaml/opcodeTable.cmx
Normal file
Binary file not shown.
159
ass2/SICocaml/opcodeTable.ml
Normal file
159
ass2/SICocaml/opcodeTable.ml
Normal file
|
|
@ -0,0 +1,159 @@
|
|||
(*opcode hash table, formati ukazov in mnemoniki*)
|
||||
type mnemonic =
|
||||
(* Format 1 *)
|
||||
| FIX | FLOAT | HIO | NORM | SIO | TIO
|
||||
|
||||
(* Format 2 *)
|
||||
| ADDR | CLEAR | COMPR | DIVR | MULR | RMO
|
||||
| SHIFTL | SHIFTR | SUBR | SVC | TIXR
|
||||
|
||||
(* Format 3/4 *)
|
||||
| ADD | ADDF | AND | COMP | COMPF | DIV
|
||||
| J | JEQ | JGT | JLT | JSUB | LDA | LDB | LDCH | LDF
|
||||
| LDL | LDS | LDT | LDX | LPS | MUL | OR | RD
|
||||
| RSUB | STA | STB | STCH | STF | STL | STS | STSW
|
||||
| STT | STX | SUB | SUBF | TD | TIX | WD
|
||||
|
||||
type format =
|
||||
| F1
|
||||
| F2
|
||||
| F3_4
|
||||
|
||||
type info = {
|
||||
mnemonic : mnemonic;
|
||||
format : format;
|
||||
}
|
||||
|
||||
(* Opcode table *)
|
||||
|
||||
let table : (int, info) Hashtbl.t = Hashtbl.create 128
|
||||
|
||||
let () =
|
||||
let add op mnemonic format =
|
||||
Hashtbl.add table op { mnemonic; format }
|
||||
in
|
||||
|
||||
(*Format 1 Instructions*)
|
||||
add 0xC4 FIX F1;
|
||||
add 0xC0 FLOAT F1;
|
||||
add 0xC8 HIO F1;
|
||||
add 0xC1 NORM F1;
|
||||
add 0xF0 SIO F1;
|
||||
add 0xF8 TIO F1;
|
||||
|
||||
(*Format 2 Instructions*)
|
||||
add 0x90 ADDR F2;
|
||||
add 0xB4 CLEAR F2;
|
||||
add 0xA0 COMPR F2;
|
||||
add 0x9C DIVR F2;
|
||||
add 0x98 MULR F2;
|
||||
add 0xAC RMO F2;
|
||||
add 0xA4 SHIFTL F2;
|
||||
add 0xA8 SHIFTR F2;
|
||||
add 0x94 SUBR F2;
|
||||
add 0xB0 SVC F2;
|
||||
add 0xB8 TIXR F2;
|
||||
|
||||
(*Format 3/4 Instructions*)
|
||||
add 0x18 ADD F3_4;
|
||||
add 0x58 ADDF F3_4;
|
||||
add 0x40 AND F3_4;
|
||||
add 0x28 COMP F3_4;
|
||||
add 0x88 COMPF F3_4;
|
||||
add 0x24 DIV F3_4;
|
||||
add 0x3C J F3_4;
|
||||
add 0x30 JEQ F3_4;
|
||||
add 0x34 JGT F3_4;
|
||||
add 0x38 JLT F3_4;
|
||||
add 0x48 JSUB F3_4;
|
||||
add 0x00 LDA F3_4;
|
||||
add 0x68 LDB F3_4;
|
||||
add 0x50 LDCH F3_4;
|
||||
add 0x70 LDF F3_4;
|
||||
add 0x08 LDL F3_4;
|
||||
add 0x6C LDS F3_4;
|
||||
add 0x74 LDT F3_4;
|
||||
add 0x04 LDX F3_4;
|
||||
add 0xD0 LPS F3_4;
|
||||
add 0x20 MUL F3_4;
|
||||
add 0x44 OR F3_4;
|
||||
add 0xD8 RD F3_4;
|
||||
add 0x4C RSUB F3_4;
|
||||
add 0x0C STA F3_4;
|
||||
add 0x78 STB F3_4;
|
||||
add 0x54 STCH F3_4;
|
||||
add 0x80 STF F3_4;
|
||||
add 0x14 STL F3_4;
|
||||
add 0x7C STS F3_4;
|
||||
add 0xE8 STSW F3_4;
|
||||
add 0x84 STT F3_4;
|
||||
add 0x10 STX F3_4;
|
||||
add 0x1C SUB F3_4;
|
||||
add 0x5C SUBF F3_4;
|
||||
add 0xE0 TD F3_4;
|
||||
add 0x2C TIX F3_4;
|
||||
add 0xDC WD F3_4;
|
||||
()
|
||||
|
||||
(*mnemonic to string*)
|
||||
let string_of_mnemonic = function
|
||||
(* Format 1 *)
|
||||
| FIX -> "FIX"
|
||||
| FLOAT -> "FLOAT"
|
||||
| HIO -> "HIO"
|
||||
| NORM -> "NORM"
|
||||
| SIO -> "SIO"
|
||||
| TIO -> "TIO"
|
||||
|
||||
(* Format 2 *)
|
||||
| ADDR -> "ADDR"
|
||||
| CLEAR -> "CLEAR"
|
||||
| COMPR -> "COMPR"
|
||||
| DIVR -> "DIVR"
|
||||
| MULR -> "MULR"
|
||||
| RMO -> "RMO"
|
||||
| SHIFTL -> "SHIFTL"
|
||||
| SHIFTR -> "SHIFTR"
|
||||
| SUBR -> "SUBR"
|
||||
| SVC -> "SVC"
|
||||
| TIXR -> "TIXR"
|
||||
|
||||
(* Format 3/4 *)
|
||||
| ADD -> "ADD"
|
||||
| ADDF -> "ADDF"
|
||||
| AND -> "AND"
|
||||
| COMP -> "COMP"
|
||||
| COMPF -> "COMPF"
|
||||
| DIV -> "DIV"
|
||||
| J -> "J"
|
||||
| JEQ -> "JEQ"
|
||||
| JGT -> "JGT"
|
||||
| JLT -> "JLT"
|
||||
| JSUB -> "JSUB"
|
||||
| LDA -> "LDA"
|
||||
| LDB -> "LDB"
|
||||
| LDCH -> "LDCH"
|
||||
| LDF -> "LDF"
|
||||
| LDL -> "LDL"
|
||||
| LDS -> "LDS"
|
||||
| LDT -> "LDT"
|
||||
| LDX -> "LDX"
|
||||
| LPS -> "LPS"
|
||||
| MUL -> "MUL"
|
||||
| OR -> "OR"
|
||||
| RD -> "RD"
|
||||
| RSUB -> "RSUB"
|
||||
| STA -> "STA"
|
||||
| STB -> "STB"
|
||||
| STCH -> "STCH"
|
||||
| STF -> "STF"
|
||||
| STL -> "STL"
|
||||
| STS -> "STS"
|
||||
| STSW -> "STSW"
|
||||
| STT -> "STT"
|
||||
| STX -> "STX"
|
||||
| SUB -> "SUB"
|
||||
| SUBF -> "SUBF"
|
||||
| TD -> "TD"
|
||||
| TIX -> "TIX"
|
||||
| WD -> "WD"
|
||||
BIN
ass2/SICocaml/opcodeTable.o
Normal file
BIN
ass2/SICocaml/opcodeTable.o
Normal file
Binary file not shown.
BIN
ass2/SICocaml/program
Normal file
BIN
ass2/SICocaml/program
Normal file
Binary file not shown.
Loading…
Add table
Add a link
Reference in a new issue