working on ass3, todo pc, nixbpe

This commit is contained in:
Jaka Furlan 2025-12-14 23:31:52 +01:00
parent 6261d9fe37
commit beabcde7db
15 changed files with 412 additions and 194 deletions

View file

@ -117,6 +117,9 @@ type lineSemantic = {
ext : bool; (*za razlikovanje med F3 in F4*)
opcode : mnemonic;
mnem : mnemonic_type;
x : bool; (*ali imamo X naslavljanje*)
n : bool;
i : bool;
comment : string option;
len : int; (*dolžina, pove za koliko moramo povečati locctr*)
mutable loc : int option; (* assigned in pass1 *)
@ -130,78 +133,30 @@ let getMnem (opcode : string) : mnemonic * bool =
let o =
match opcode with
(* ------------------- Format 1 instructions ------------------- *)
| "FIX" -> FIX
| "FLOAT" -> FLOAT
| "HIO" -> HIO
| "NORM" -> NORM
| "SIO" -> SIO
| "TIO" -> TIO
| "FIX" -> FIX | "FLOAT" -> FLOAT | "HIO" -> HIO
| "NORM" -> NORM | "SIO" -> SIO | "TIO" -> TIO
(* ------------------- Format 2 instructions ------------------- *)
| "ADDR" -> ADDR
| "CLEAR" -> CLEAR
| "COMPR" -> COMPR
| "DIVR" -> DIVR
| "MULR" -> MULR
| "RMO" -> RMO
| "SHIFTL" -> SHIFTL
| "SHIFTR" -> SHIFTR
| "SUBR" -> SUBR
| "SVC" -> SVC
| "TIXR" -> TIXR
| "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 instructions ------------------- *)
| "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
| "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
(* ------------------- SIC/XE Directives ------------------- *)
| "START" -> START
| "END" -> END
| "ORG" -> ORG
| "EQU" -> EQU
| "BASE" -> BASE
| "NOBASE" -> NOBASE
| "LTORG" -> LTORG
| "RESW" -> RESW
| "RESB" -> RESB
| "WORD" -> WORD
| "BYTE" -> BYTE
| "START" -> START | "END" -> END | "ORG" -> ORG
| "EQU" -> EQU | "BASE" -> BASE | "NOBASE" -> NOBASE
| "LTORG" -> LTORG | "RESW" -> RESW | "RESB" -> RESB
| "WORD" -> WORD | "BYTE" -> BYTE
(*comment*)
| "COMMENT" -> COMMENT
@ -281,6 +236,7 @@ let getMnemType (opcode : mnemonic) (ext : bool) (operand : string list) : mnemo
| _, _ -> failwith (Printf.sprintf "Invalid operands for opcode %s: %d operands"
(string_of_mnemonic opcode) (List.length operand))
(*vrni dolzino ukaza*)
let getLen (mnemType : mnemonic_type) (mnem : mnemonic) : int =
match mnemType with
(* ----------- directives with no memory ----------- *)
@ -316,11 +272,48 @@ let getLen (mnemType : mnemonic_type) (mnem : mnemonic) : int =
| _ -> failwith "Narobe mnemoic glede na mnemType"
end
(*preveri ali imaš X naslavljanje*)
let has_index (ops : string list) : bool * string list = (*vrne bool x in operande brez X*)
match ops with
| [lst; x] when String.uppercase_ascii x = "X" -> true, [lst] (*predpostavimo da se X pojavi na drugem mestu in nikoli na tretjem*)
| _ -> false, ops
(*dobi n in i bita in operande brez # ali @*)
let get_ni_bits (operands : string list) : bool * bool * string list =
match operands with
| [] ->
(true, true, [])
| op :: rest ->
if String.length op = 0 then
(true, true, operands)
else
match op.[0] with
| '#' ->
let stripped =String.sub op 1 (String.length op - 1) in
(false, true, stripped :: rest)
| '@' ->
let stripped = String.sub op 1 (String.length op - 1) in
(true, false, stripped :: rest)
| _ ->
(true, true, operands)
let checkLineSemantic (line : line) : lineSemantic =
let mnem, ext = getMnem line.opcode in
let mnemType = getMnemType mnem ext line.operand in
let x, ops = has_index line.operand in
let n, i, ops = get_ni_bits ops in
let mnemType = getMnemType mnem ext ops in
let dolzina = getLen mnemType mnem in
{line_no = line.line_no; label = line.label; ext = ext; opcode = mnem; mnem = mnemType; comment = line.comment; len = dolzina; loc = line.loc}
{ line_no = line.line_no;
label = line.label;
ext = ext;
opcode = mnem;
mnem = mnemType;
x = x;
n = n;
i = i;
comment = line.comment;
len = dolzina;
loc = line.loc}
let checkLineSemanticOfCode (code : line list) : lineSemantic list =
List.map (checkLineSemantic) code