-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCesarCmdLine.fs
More file actions
80 lines (64 loc) · 3.71 KB
/
Copy pathCesarCmdLine.fs
File metadata and controls
80 lines (64 loc) · 3.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
namespace Ufrgs.Inf.ArchSims.CmdLine
open System
open System.IO
open Ufrgs.Inf.ArchSims.Core.Debugger
open Ufrgs.Inf.ArchSims.Core.Cesar
open Ufrgs.Inf.ArchSims.Assemblers.Cesar.CesarAssembler
open Ufrgs.Inf.ArchSims.CmdLine.Common
module CesarCmdLine =
let Run options =
let printCpu cpu =
let printFlags character value =
if value then character else "-"
let flags = printFlags "N" cpu.Registers.Flags.Negative +
printFlags "Z" cpu.Registers.Flags.Zero +
printFlags "V" cpu.Registers.Flags.Overflow +
printFlags "C" cpu.Registers.Flags.Carry
let ir, _ = DisassembleInstruction (List.ofArray cpu.Registers.InstructionRegister.Data)
let toBinary (value) =
Int64.Parse(Convert.ToString(int value, 2))
if (options.ExecutionMode <> ExecutionMode.LastStep) || (cpu.Registers.Flags.Halted) then
let r0 = cpu.Registers.R.[0]
let r1 = cpu.Registers.R.[1]
let r2 = cpu.Registers.R.[2]
let r3 = cpu.Registers.R.[3]
let r4 = cpu.Registers.R.[4]
let r5 = cpu.Registers.R.[5]
let r6 = cpu.Registers.R.[6]
let r7 = cpu.Registers.R.[7]
match options.OutputFormat with
| OutputFormat.Decimal -> printf "R0:%5u R1:%5u R2:%5u R3:%5u R4:%5u R5:%5u R6:%5u PC:%5u IR: [%-28s] %s" r0 r1 r2 r3 r4 r5 r6 r7 ir flags
| OutputFormat.Hexadecimal -> printf "R0:%4X R1:%4X R2:%4X R3:%4X R4:%4X R5:%4X R6:%4X PC:%4X IR: [%-28s] %s" r0 r1 r2 r3 r4 r5 r6 r7 ir flags
| OutputFormat.Binary -> printf "R0:%016u R1:%016u R2:%016u R3:%016u\nR4:%016u R5:%016u R6:%016u PC:%016u\nIR: [%-28s] %s" (toBinary r0) (toBinary r1) (toBinary r2) (toBinary r3) (toBinary r4) (toBinary r5) (toBinary r6) (toBinary r7) ir flags
if (options.ExecutionMode = ExecutionMode.Interactive) && (not cpu.Registers.Flags.Halted) then
match options.OutputFormat with
| OutputFormat.Binary -> Console.SetCursorPosition(0, Console.CursorTop - 2)
| _ -> Console.SetCursorPosition(0, Console.CursorTop)
match options.Speed with
| Some s when s > 0 && s < 1000 -> Async.RunSynchronously (async { do! Async.Sleep (int (1000 / s)) })
| _ -> ()
else
printfn ""
let cpu = CreateCpu()
let debugger = CreateDebugger (fun () -> int cpu.Registers.R.[7])
(fun () -> Step cpu
printCpu cpu
cpu.Registers.Flags.Halted)
AssembleProgram cpu (File.ReadAllText options.SourceFileName)
printCpu cpu
DebuggerRun debugger 1000
let stopReason = match debugger.LastStop with
| DebuggerStopReason.Breakpoint -> "Breakpoint hit"
| DebuggerStopReason.Halted -> "Program halted"
| DebuggerStopReason.RunningForever -> "Running forever..."
| _ -> ""
printfn ""
printfn "Finished: %s" stopReason
let Save options =
let cpu = CreateCpu()
AssembleProgram cpu (File.ReadAllText options.SourceFileName)
MemorySaveToFile cpu.Memory [0x03uy; 0x43uy; 0x31uy; 0x36uy] (* ^CC16 *) options.TargetFileName
let Execute action =
match action with
| Run options -> Run options
| Save options -> Save options