Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
- run :: InstImage -> DataImage -> CpuState
- data Inst
- = NOP
- | HALT
- | MOVI GReg Int
- | MOV GReg GReg
- | MOVPC GReg
- | ADD GReg GReg GReg
- | SUB GReg GReg GReg
- | CMP GReg GReg
- | ABS GReg GReg
- | ASH GReg GReg GReg
- | MUL GReg GReg GReg
- | DIV GReg GReg GReg
- | AND GReg GReg GReg
- | OR GReg GReg GReg
- | NOT GReg GReg
- | XOR GReg GReg GReg
- | LSH GReg GReg GReg
- | BRI FCond Int
- | JRI Int
- | J GReg
- | CALL GReg
- | RET
- | LD GReg GReg
- | ST GReg GReg
- | UNDEF
- data GReg
- data FCond
- type InstImage = [(IAddress, [Inst])]
- type DataImage = [(DAddress, [DValue])]
- data CpuState
- pcFromCpuState :: CpuState -> Int
- grFromCpuState :: CpuState -> [Int]
- flFromCpuState :: CpuState -> [Bool]
- imemFromCpuState :: CpuState -> InstImage
- dmemFromCpuState :: CpuState -> DataImage
- dumpCpuState :: CpuState -> String
Run the processor
run :: InstImage -> DataImage -> CpuState Source
run the processor
Example: simple run
> run [(0, [MOVI R0 20, HALT])] [] pc : 1 gr : [20,0,0,0,0,0,0,0] fl : [False,False] ...
Example: run with initial data
> run [(0, [MOVI R1 0, LD R0 R1, HALT])] [(0,[100])] pc : 2 gr : [100,0,0,0,0,0,0,0] fl : [False,False] ...
The instruction set
the instruction definition.
You can create instructions as you like :-)
Operand order is Intel, ARM, MIPS, PowerPC,... order. (opcode dst src1 src2)
NOP | no operation |
HALT | halt (stop the processor) |
MOVI GReg Int | GReg <- Int |
MOV GReg GReg | GReg <- GReg |
MOVPC GReg | GReg <- PC |
ADD GReg GReg GReg | GReg <- GReg + GReg |
SUB GReg GReg GReg | GReg <- GReg - GReg |
CMP GReg GReg | Flag <- compare(GReg, GReg) |
ABS GReg GReg | GReg <- abs(GReg) |
ASH GReg GReg GReg | GReg <- GReg << GReg // arithmetic shift |
MUL GReg GReg GReg | GReg <- GReg * GReg |
DIV GReg GReg GReg | GReg <- GReg / GReg |
AND GReg GReg GReg | GReg <- GReg & GReg |
OR GReg GReg GReg | GReg <- GReg | GReg |
NOT GReg GReg | GReg <- ~GReg |
XOR GReg GReg GReg | GReg <- GReg ^ GReg |
LSH GReg GReg GReg | GReg <- GReg << GReg // logical shift |
BRI FCond Int | if (FCond(Flag)) goto (PC + Int) // pc relative addressing |
JRI Int | goto (PC + Int) // pc relative addressing |
J GReg | goto GReg // absolute addressing |
CALL GReg | goto GReg; R0 <- PC // absolute addressing |
RET | goto R0 |
LD GReg GReg | GReg <- memory(GReg) |
ST GReg GReg | memory(GReg) <- GReg |
UNDEF | undefined |
the general purpose registers.
You can create registers as you like :-)
the Flag conditions
Instruction and data memory images
type InstImage = [(IAddress, [Inst])] Source
the instruction memory image
[(StartAddress, [Instruction1, Instruction2, ...])]
Example:
[(0, [MOVI R1 0, LD R0 R1, HALT])]
type DataImage = [(DAddress, [DValue])] Source
the data memory image
[(StartAddress, [Data1, Data2, ...])]
Example:
[(0, [1, 5, 7, 0x20])]
Cpu states (processor internal states)
the cpu state (processor internal state)
This is the result type from run
function.
get each values by pcFromCpuState
, grFromCpuState
, flFromCpuState
,
imemFromCpuState
, dmemFromCpuState
, dumpCpuState
pcFromCpuState :: CpuState -> Int Source
> pcFromCpuState $ run [(0,[MOVI R0 7, HALT])] [] 1
grFromCpuState :: CpuState -> [Int] Source
> grFromCpuState $ run [(0,[MOVI R0 7, HALT])] [] [7,0,0,0,0,0,0,0]
flFromCpuState :: CpuState -> [Bool] Source
> flFromCpuState $ run [(0,[MOVI R0 7, HALT])] [] [False,False]
imemFromCpuState :: CpuState -> InstImage Source
> imemFromCpuState $ run [(0,[MOVI R0 7, HALT])] [] [(0,[MOVI R0 7,HALT,UNDEF,UNDEF,...])]
dmemFromCpuState :: CpuState -> DataImage Source
> dmemFromCpuState $ run [(0,[MOVI R0 0, MOVI R1 10, ST R0 R1, HALT])] [] [(0,[10,0,0,0,0,...])]
dumpCpuState :: CpuState -> String Source
dump Cpu state (without instruction image)
> putStr $ dumpCpuState $ run [(0,[MOVI R0 7, HALT])] [] pc : 1 gr : [7,0,0,0,0,0,0,0] fl : [False,False] dm : [(0,[7,0,0,0,0,...])]