-
Notifications
You must be signed in to change notification settings - Fork 0
/
Device.v
181 lines (160 loc) · 6.62 KB
/
Device.v
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
Require Import Kami.AllNotations.
Require Import ProcKami.FU.
Require Import ProcKami.MemOps.
Require Import ProcKami.MemOpsFuncs.
Require Import ProcKami.MemRegion.
Require Import StdLibKami.Router.Ifc.
Section DeviceIfc.
Context {procParams : ProcParams}.
Local Definition Req tagK
:= STRUCT_TYPE {
"tag" :: tagK;
"memOp" :: MemOpCode;
"offset" :: Offset;
"data" :: Data
}.
Local Definition Res tagK
:= STRUCT_TYPE {
"tag" :: tagK;
"res" :: Pair Data TlSize }.
Record Device
:= {
name : string;
io : bool;
pmas : list Pma;
amo : PmaAmoClass;
regFiles: list RegFileBase;
regs: forall tagK: Kind, list RegInitT;
deviceIfc : forall tagK, DeviceIfc (Req tagK) (Res tagK) }.
Class BaseDevice := { baseName: string;
baseIo: bool;
basePmas: list Pma;
baseAmo: PmaAmoClass;
baseRegFiles: list RegFileBase;
baseRegs: list RegInitT;
write : forall ty, MemWrite @# ty -> ActionT ty Bool; (* Error *)
readReq : forall ty, Offset @# ty -> ActionT ty Void;
readRes : forall ty, ActionT ty (Maybe Data); (* Error *)
}.
Section BaseDevice.
Context (baseDevice: BaseDevice).
Local Notation "^ x" := (@^baseName ++ "." ++ x)%string (at level 0).
Section ty.
Variable tagK: Kind.
Variable ty : Kind -> Type.
Local Open Scope kami_expr.
Local Open Scope kami_action.
Local Definition memStoreValue
(code : MemOpCode @# ty)
(offset: Offset @# ty)
(regData : Data @# ty)
: ActionT ty (Maybe MemWrite)
:= applyMemOp
memOps
(fun memOp
=> (match memOpWriteValue memOp return ActionT ty (Maybe MemWrite) with
| memWriteValueFn f => Ret (Invalid : Maybe MemWrite @# ty)
| memWriteValueStore
=> Ret (Valid (STRUCT { "addr" ::= offset;
"data" ::= unpack (Array Rlen_over_8 (Bit 8)) regData;
"mask" ::= (getMask (memOpSize memOp) ty) }): Maybe MemWrite @# ty)
| memWriteValueNone => Ret (Invalid : Maybe MemWrite @# ty)
end))
code.
Local Definition memAmoValue
(code : MemOpCode @# ty)
(offset: Offset @# ty)
(regData : Data @# ty)
(memData : Data @# ty)
: ActionT ty (Maybe MemWrite)
:= applyMemOp
memOps
(fun memOp
=> (match memOpWriteValue memOp return ActionT ty (Maybe MemWrite) with
| memWriteValueFn f
=> (LETA result : Data <- convertLetExprSyntax_ActionT (f ty regData memData);
Ret (Valid (STRUCT { "addr" ::= offset;
"data" ::= unpack (Array Rlen_over_8 (Bit 8)) #result;
"mask" ::= (getMask (memOpSize memOp) ty) })
: Maybe MemWrite @# ty))
| memWriteValueStore => Ret (Invalid : Maybe MemWrite @# ty)
| memWriteValueNone => Ret (Invalid : Maybe MemWrite @# ty)
end))
code.
Local Definition sendReq
(req : ty (Req tagK))
: ActionT ty Bool (* accepted *)
:= Read busy : Bool <- ^"busy";
If !#busy
then (
LETA writeData : Maybe MemWrite <- memStoreValue
(#req @% "memOp")
(#req @% "offset")
(#req @% "data");
(* LETA hasLoad <- memOpHasLoad memOps (#req @% "memOp"); *)
If #writeData @% "valid" (* && !#hasLoad *)
then (
LETA _ <- write (#writeData @% "data");
Retv )
else (
Write ^"busy" : Bool <- $$true;
Write ^"req" : Req tagK <- #req;
readReq (#req @% "offset"));
Retv);
Ret !#busy.
(*
* Invalid - not accepted
*)
Local Definition getRes
: ActionT ty (Maybe (Res tagK))
:= Read req : Req tagK <- ^"req";
Read busy : Bool <- ^"busy";
If #busy
then
LETA hasLoad : Bool <- memOpHasLoad memOps (#req @% "memOp");
LETA memData: Maybe Data <- readRes ty;
LETA writeData : Maybe MemWrite <- memAmoValue
(#req @% "memOp")
(#req @% "offset")
(#req @% "data") (#memData @% "data");
If #writeData @% "valid"
then write (#writeData @% "data");
Write ^"busy": Bool <- $$ false;
LET result
: (Res tagK) <-
STRUCT { "tag" ::= #req @% "tag";
"res" ::= STRUCT { "fst" ::= ((#memData @% "data") <<
(getByteShiftAmt (getSize (#req @% "memOp")) (#req @% "offset")));
"snd" ::= getSize (#req @% "memOp") } };
Ret #result
else
Ret $$(getDefaultConst (Res tagK)) as res;
LET retval <- ((STRUCT {"valid" ::= #busy;
"data" ::= #res }): Maybe (Res tagK) @# ty);
Ret #retval.
Local Close Scope kami_action.
Local Close Scope kami_expr.
End ty.
Definition createDevice :=
{| name := baseName;
io := baseIo;
pmas := basePmas;
amo := baseAmo;
regFiles:= baseRegFiles;
regs := fun tagK =>
makeModule_regs
((Register ^"busy": Bool <- Default)
++ Register ^"req": Req tagK <- Default)%kami
++ baseRegs;
deviceIfc := fun tagK =>
{| deviceReq := sendReq tagK ;
devicePoll := getRes tagK |};
|}.
End BaseDevice.
Record DeviceTree
:= {
devices : list Device;
memTable : list (@MemTableEntry (length devices));
memTableIsValid : (@memRegions (length devices) memTable) <> []
}.
End DeviceIfc.