-
Notifications
You must be signed in to change notification settings - Fork 0
/
Monad.hs
327 lines (296 loc) · 6.44 KB
/
Monad.hs
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
{-# LANGUAGE DefaultSignatures #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE Rank2Types #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeOperators #-}
module HOCD.Monad
( OCDT
, runOCDT
, MonadOCD(..)
, halt
, halt'
, reset
, resetHalt
, resetHaltInit
, resume
, resumeAt
, step
, stepTo
, readMem
, readMem32
, readMemCount
, writeMem
, writeMem32
, registers
, readReg
, writeReg
, version
, raw
) where
import Control.Monad.Catch (MonadCatch, MonadMask, MonadThrow)
import Control.Monad.Except (MonadError, throwError)
import Control.Monad.IO.Class (MonadIO(liftIO))
import Control.Monad.Reader (MonadReader, ask)
import Control.Monad.Trans (MonadTrans, lift)
import Control.Monad.Trans.Except (ExceptT, runExceptT)
import Control.Monad.Trans.Reader (ReaderT, runReaderT)
import Control.Monad.Trans.State (StateT)
import Data.Bits (FiniteBits(..))
import Data.ByteString (ByteString)
import Data.Map (Map)
import Data.Word (Word32)
import HOCD.Command
( Command(..)
, Capture(..)
, Halt(..)
, Resume(..)
, Reset(..)
, ResetMode(..)
, Step(..)
, ReadMemory(..)
, WriteMemory(..)
, Registers(..)
, ReadRegister(..)
, WriteRegister(..)
, Version(..)
, Raw(..)
, subChar
)
import HOCD.Error (OCDError(..))
import HOCD.Types (MemAddress, RegisterInfo, RegisterName)
import Network.Socket (Socket)
import Text.Printf (PrintfArg)
import qualified Data.ByteString.Char8
import qualified Network.Socket.ByteString
newtype OCDT m a = OCDT
{ _unOCDT
:: ExceptT OCDError
(ReaderT Socket m) a
}
deriving
( Functor
, Applicative
, Monad
, MonadReader Socket
, MonadError OCDError
, MonadCatch
, MonadMask
, MonadThrow
, MonadIO
)
instance MonadTrans OCDT where
lift = OCDT . lift . lift
-- | Run OCDT transformer
runOCDT
:: Monad m
=> Socket
-> OCDT m a
-> m (Either OCDError a)
runOCDT sock =
(`runReaderT` sock)
. runExceptT
. _unOCDT
class ( MonadIO m
, MonadError OCDError m
) => MonadOCD m where
getSocket :: m Socket
default getSocket
:: ( MonadTrans t
, MonadOCD m'
, m ~ t m'
)
=> m Socket
getSocket = lift getSocket
instance MonadIO m => MonadOCD (OCDT m) where
getSocket = ask
instance MonadOCD m => MonadOCD (StateT s m)
instance MonadOCD m => MonadOCD (ReaderT r m)
instance MonadOCD m => MonadOCD (ExceptT OCDError m)
-- | Perform RPC call
rpc
:: ( MonadOCD m
, Command req
)
=> req
-> m (Reply req)
rpc cmd = do
sock <- getSocket
liftIO $
Network.Socket.ByteString.sendAll
sock
(rpcCmd $ request cmd)
reply cmd <$> recvTillSub sock
>>= either throwError pure
where
recvTillSub s = do
msg <-
liftIO
$ Network.Socket.ByteString.recv
s
1024
if Data.ByteString.Char8.last msg == subChar
then pure msg
else recvTillSub s >>= pure . (msg <>)
-- | Terminate with \SUB
rpcCmd :: ByteString -> ByteString
rpcCmd =
(<> Data.ByteString.Char8.singleton subChar)
-- | Halt target
halt
:: MonadOCD m
=> m ByteString
halt = rpc $ Capture Halt
-- | Halt target, discarding reply
halt'
:: MonadOCD m
=> m ()
halt' = halt >> pure ()
-- | Reset target (default "reset run")
reset
:: MonadOCD m
=> m ()
reset = rpc $ Reset ResetMode_Run
-- | Reset target and halt execution
resetHalt
:: MonadOCD m
=> m ()
resetHalt = rpc $ Reset ResetMode_Halt
-- | Reset target, halt execution
-- and execute reset-init script
resetHaltInit
:: MonadOCD m
=> m ()
resetHaltInit = rpc $ Reset ResetMode_Init
-- | Resume execution
resume
:: MonadOCD m
=> m ()
resume = rpc $ Resume Nothing
-- | Resume execution at @MemAddress@
resumeAt
:: MonadOCD m
=> MemAddress
-> m ()
resumeAt = rpc . Resume . Just
-- | Single-step target at its current code position
step
:: MonadOCD m
=> m ()
step = rpc $ Step Nothing
-- | Single-step target to code position
-- at @MemAddress@
stepTo
:: MonadOCD m
=> MemAddress
-> m ()
stepTo = rpc . Step . Just
-- | Read multiple memory segments from @MemAddress@
-- according to count argument. Segment size depends
-- on Word type.
readMemCount
:: forall a m
. ( MonadOCD m
, FiniteBits a
, Integral a
)
=> MemAddress -- ^ Memory address to read from
-> Int -- ^ Count
-> m [a]
readMemCount ma c =
rpc
ReadMemory
{ readMemoryAddr = ma
, readMemoryCount = c
}
-- | Read single memory segment from @MemAddress@
-- Segment size depends on Word type.
readMem
:: forall a m
. ( MonadOCD m
, FiniteBits a
, Integral a
)
=> MemAddress -- ^ Memory address to read from
-> m a
readMem ma =
readMemCount ma 1
>>= \case
[one] -> pure one
_ -> throwError OCDError_ExpectedOneButGotMore
-- | Shorthand for reading @Word32@ sized segment
readMem32
:: MonadOCD m
=> MemAddress -- ^ Memory address to read from
-> m Word32
readMem32 = readMem @Word32
-- | Write multiple memory segments to @MemAddress@
writeMem
:: forall a m
. ( MonadOCD m
, FiniteBits a
, PrintfArg a
, Integral a
)
=> MemAddress -- ^ Memory address to write to
-> [a] -- ^ Data to write
-> m ()
writeMem ma xs =
rpc
WriteMemory
{ writeMemoryAddr = ma
, writeMemoryData = xs
}
-- | Shorthand for writing @Word32@ sized segment
writeMem32
:: MonadOCD m
=> MemAddress -- ^ Memory address to write to
-> [Word32] -- ^ Data to write
-> m ()
writeMem32 = writeMem @Word32
registers
:: MonadOCD m
=> m (Map RegisterName RegisterInfo)
registers = rpc Registers
-- | Read a CPU register
readReg
:: forall a m
. ( MonadOCD m
, FiniteBits a
, Integral a
)
=> RegisterName -- ^ Name of the register to query
-> m a
readReg = rpc . ReadRegister
-- | Write a CPU register
writeReg
:: forall a m
. ( MonadOCD m
, FiniteBits a
, Integral a
, PrintfArg a
)
=> RegisterName -- ^ Name of the register to write to
-> a -- ^ Value to write
-> m ()
writeReg rn x =
rpc
$ WriteRegister
{ writeRegisterName = rn
, writeRegisterValue = x
}
-- | Query OpenOCD version
version
:: MonadOCD m
=> m ByteString
version = rpc Version
-- | Send raw OpenOCD command
-- Escape hatch for commands that are not
-- part defined as part of hocd api
raw
:: MonadOCD m
=> ByteString
-> m ByteString
raw = rpc . Raw