-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestDriver.bs
160 lines (137 loc) · 5.55 KB
/
TestDriver.bs
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package TestDriver where
import Driver
import ChessState
import MoveEval
import MoveUpdate
import SearchCore
import ShallowSearchCores
import DefaultHeuristic
import GenCMsg
import GenCRepr
import GetPut
import ClientServer
import Connectable
import COBS
import CShow
import FIFOF
import Vector
type InitMoves = 2
data Command
= Config { depth :: Depth; white :: Config; black :: Config }
| RunTrial { rid :: UInt 8; initMoves :: Vector InitMoves (UInt 8); }
deriving (Bits)
data TrialOutcome = Win Color | Draw | Error
deriving (Eq, Bits)
struct TrialResult =
rid :: (UInt 8)
outcome :: TrialOutcome
deriving (Bits)
interface ChessTestMsgs =
command :: Rx 1 1 Command
result :: Tx 8 8 TrialResult
{-# verilog mkChessTestDriver #-}
mkChessTestDriver :: Module Driver
mkChessTestDriver = _mkChessTestDriver
-- Seperate due to the context
_mkChessTestDriver :: (GenCMsg ChessTestMsgs rxBytes txBytes) => Module Driver
_mkChessTestDriver = module
writeCMsgDecls "chess_test" (_ :: ChessTestMsgs)
enc :: COBSEncoder txBytes <- mkCOBSEncoder
dec :: COBSDecoder rxBytes <- mkCOBSDecoder
msgMgr :: MsgManager ChessTestMsgs rxBytes txBytes <- mkMsgManager
dec.msg <-> dropSize msgMgr.rxMsg
msgMgr.txMsg <-> enc.msg
stateUpdates :: FIFOF State <- mkFIFOF
state :: Reg State <- mkReg initialState
moveUpdate <- mkMoveUpdate
searchCore <- mkShallowParallelSearchCore
whiteConfig :: Reg Config <- mkReg defaultValue
blackConfig :: Reg Config <- mkReg defaultValue
depth :: Reg Depth <- mkReg 5
rid :: Reg (UInt 8) <- mkReg 0
trialRunning :: Reg Bool <- mkReg False
numInitMoves :: Reg (UInt (TAdd 1 (TLog InitMoves))) <- mkReg 0
initMoves :: Vector InitMoves (Reg (UInt 8)) <- replicateM $ mkReg _
let config = if state.turn == White then whiteConfig else blackConfig
initMoveIndex = initMoves `select` (numInitMoves - 1)
addRules $
rules
{-# ASSERT fire when enabled #-}
"get_update_result": when True ==> do
stateUpdates.enq moveUpdate.nextState
moveUpdate.deq
`rJoinDescendingUrgency`
rules
when not trialRunning
rules
"config": when Config {depth=newDepth; white; black;} <- msgMgr.fifos.command.first ==> do
$display "config " newDepth " " (cshow white) " " (cshow black)
depth := newDepth
whiteConfig := white
blackConfig := black
msgMgr.fifos.command.deq
"start_trial": when RunTrial {rid=trialRid; initMoves=trialInitMoves;} <- msgMgr.fifos.command.first ==> do
$display "start_trial " (cshow trialRid) " " (cshow trialInitMoves)
rid := trialRid
joinActions $ zipWith writeReg initMoves trialInitMoves
trialRunning := True
numInitMoves := fromInteger (valueOf InitMoves)
stateUpdates.enq initialState
msgMgr.fifos.command.deq
when trialRunning
rules
"update_state": when True ==> do
$display "update_state"
state := stateUpdates.first
if numInitMoves > 0
then searchCore.server.request.put $ defaultValue {rid=extend numInitMoves; state=stateUpdates.first; depth=1; getMoves=True;}
else searchCore.server.request.put $ defaultValue {rid=0; state=stateUpdates.first; depth=depth; config=config}
stateUpdates.deq
when not stateUpdates.notEmpty
rules
when numInitMoves > 0
rules
when NextMove m <- searchCore.moves.first
rules
"do_initial_move": when initMoveIndex == 0 ==> do
$display "do_initial_move " initMoveIndex
moveUpdate.enq state m
searchCore.clear
numInitMoves := numInitMoves - 1
"skip_initial_move": when initMoveIndex > 0 ==> do
$display "skip_initial_move " initMoveIndex
searchCore.moves.deq
initMoveIndex := initMoveIndex - 1
-- initMoveIndex was larger than the number of moves
"error_initial_move": when NoMove <- searchCore.moves.first ==> do
$display "error_initial_move " initMoveIndex
searchCore.clear
msgMgr.fifos.result.enq $ TrialResult {rid=rid; outcome=Error}
trialRunning := False
"do_search_move": when numInitMoves == 0 ==> do
result <- searchCore.server.response.get
$display "do_search_move " (cshow result)
case result.outcome of
CheckMate -> do
trialRunning := False
msgMgr.fifos.result.enq $ TrialResult {rid=rid; outcome=Win state.turn}
Draw -> do
trialRunning := False
msgMgr.fifos.result.enq $ TrialResult {rid=rid; outcome=Draw}
_ ->
case result.bestMove of
Just move -> moveUpdate.enq state move
Nothing -> do
trialRunning := False
msgMgr.fifos.result.enq $ TrialResult {rid=rid; outcome=Error}
searchCore.clear
interface
txData = enc.byte
rxData = dec.byte
status = searchCore.status
{-# verilog mkTestTop #-}
mkTestTop :: Module Top
mkTestTop = mkHwTop mkChessTestDriver
{-# verilog sysChessTestSim #-}
sysChessTestSim :: Module Empty
sysChessTestSim = mkSimTop mkChessTestDriver