Skip to content

Commit

Permalink
Implement GOF in visa simulator
Browse files Browse the repository at this point in the history
  • Loading branch information
mbarbin committed Feb 9, 2024
1 parent 28585c0 commit 11d7749
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

### Fixed

- Fixed behavior of the `GOF` instruction in the visa simulator.

### Removed

## 0.2.5 (2024-02-09)
Expand Down
11 changes: 10 additions & 1 deletion project/visa/lib/visa_simulator/src/memory.ml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ end
type t =
{ mutable register_R0 : int
; mutable register_R1 : int
; mutable overflow_flag : bool
; output_device : Output_device.t
; memory : Ram.t
}
Expand All @@ -33,6 +34,7 @@ type t =
let create () =
{ register_R0 = 0
; register_R1 = 0
; overflow_flag = false
; memory = Ram.create ()
; output_device = Output_device.create ~len:8
}
Expand Down Expand Up @@ -88,7 +90,14 @@ let write t ~register_name ~address =
return ()
;;

let add t = t.register_R1 <- (t.register_R0 + t.register_R1) mod 256
let add t =
let result = t.register_R0 + t.register_R1 in
t.overflow_flag <- result > 255;
t.register_R1 <- result % 256
;;

let overflow_flag t = t.overflow_flag
let gof t = t.register_R1 <- (if t.overflow_flag then 1 else 0)
let and_ t = t.register_R1 <- t.register_R0 land t.register_R1

let switch t =
Expand Down
18 changes: 18 additions & 0 deletions project/visa/lib/visa_simulator/src/memory.mli
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,21 @@ val switch : t -> unit
val cmp : t -> unit

val not_ : t -> register_name:Visa.Register_name.t -> unit

(** The [overflow_flag] function returns a boolean value indicating whether the
last arithmetic operation performed on the registers resulted in an
overflow. Currently, the only operation that can cause an overflow is
"add".
Specifically, the [overflow_flag] is initially set to [false]. It is updated
every time the [add] operation is performed: if the result of the addition
strictly exceeds 255, the flag is set to [true], indicating an overflow;
otherwise, it is reset to [false].
This function provides a way to check the current state of the overflow
flag. *)
val overflow_flag : t -> bool

(** [gof t] ("Get Overflow Flag") sets [R1] to the current value of the
[overflow_flag] (1 for true and 0 for false). *)
val gof : t -> unit
4 changes: 1 addition & 3 deletions project/visa/lib/visa_simulator/src/visa_simulator.ml
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,7 @@ let execute_instruction t ~instruction =
Memory.not_ t.memory ~register_name;
return (increment_code_pointer t)
| Gof ->
(* CR mbarbin: Gof is not used in the calendar, and implemented as Nop in
the simulator. It's supposed to set R1 with the overflow_flag of the last
register operation, that is set R1 to 1 iif [Add] produced a carry. *)
Memory.gof t.memory;
return (increment_code_pointer t)
| (Jmp { label } | Jmn { label } | Jmz { label }) as jump_instruction ->
let%bind code_pointer =
Expand Down

0 comments on commit 11d7749

Please sign in to comment.