Skip to content

Instantly share code, notes, and snippets.

@trygvis
Created May 7, 2011 16:41
Show Gist options
  • Save trygvis/960631 to your computer and use it in GitHub Desktop.
Save trygvis/960631 to your computer and use it in GitHub Desktop.
My first CPU
module Blaze where
import CLasH.HardwareTypes
type Word = Signed D4
type RegisterIndex = Index D4
data Instruction =
Nop
| In RegisterIndex
| Out RegisterIndex
| Add RegisterIndex RegisterIndex RegisterIndex
data BlazeR = R {
registers :: MemState D4 Word
}
type BlazeS = State BlazeR
blazeInit :: BlazeR
blazeInit = R { registers = State (vcopy 0) }
{-# ANN blaze TopEntity #-}
blaze :: BlazeS -> (Instruction, Word) -> (BlazeS, Word)
blaze (State s) (instruction, inWord) = (State s', outWord)
where
regs = registers s
(s', outWord) = case instruction of
Nop -> (s, 0)
In index -> (R { registers = regs' }, 0)
where regs' = writeBlockRAM regs inWord index
Out index -> (s, outWord)
where outWord = readBlockRAM regs index
Add x y z -> (R { registers = regs' }, 0)
where
a = readBlockRAM regs x
b = readBlockRAM regs y
regs' = writeBlockRAM regs (a + b) z
program1 :: [(Instruction, Word)]
program1 = [
(In 0, 1)
, (In 1, 2)
, (Add 0 1 2, 0)
, (Out 2, 0)
]
sim p = simulate (blaze ^^^ blazeInit) p
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment