-
Notifications
You must be signed in to change notification settings - Fork 4
/
CharIO.bsv
179 lines (161 loc) · 5.41 KB
/
CharIO.bsv
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
/*-
* Copyright (c) 2018-2021 Alexandre Joannou
* All rights reserved.
*
* This software was developed by SRI International and the University of
* Cambridge Computer Laboratory (Department of Computer Science and
* Technology) under DARPA contract HR0011-18-C-0016 ("ECATS"), as part of the
* DARPA SSITH research programme.
*
* @BERI_LICENSE_HEADER_START@
*
* Licensed to BERI Open Systems C.I.C. (BERI) under one or more contributor
* license agreements. See the NOTICE file distributed with this work for
* additional information regarding copyright ownership. BERI licenses this
* file to you under the BERI Hardware-Software License, Version 1.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
*
* http://www.beri-open-systems.org/legal/license-1-0.txt
*
* Unless required by applicable law or agreed to in writing, Work distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* @BERI_LICENSE_HEADER_END@
*/
package CharIO;
import FIFOF :: *;
import SpecialFIFOs :: *;
import AXI4Lite :: *;
import SourceSink :: *;
import MasterSlave :: *;
// C imports
import "BDPI"
function ActionValue #(Bit #(64)) serv_socket_create ( String name
, Bit #(32) dflt_port );
import "BDPI" function Action serv_socket_init (Bit #(64) ptr);
import "BDPI"
function ActionValue #(Bit #(32)) serv_socket_get8 (Bit #(64) ptr);
import "BDPI"
function ActionValue #(Bool) serv_socket_put8 (Bit #(64) ptr, Bit #(8) b);
// simple Slave interface
////////////////////////////////////////////////////////////////////////////////
typedef Tuple2 #(Sink #(Bit #(8)), Source #(Bit#(8))) CharIO;
module mkSocketCharIO #(String name, Integer dflt_port) (CharIO);
// prepare C socket
Reg #(Bool) is_initialized <- mkReg (False);
Reg #(Bit #(64)) serv_socket_ptr <- mkRegU;
rule do_init (!is_initialized);
let tmp <- serv_socket_create (name, fromInteger (dflt_port));
serv_socket_init (tmp);
serv_socket_ptr <= tmp;
is_initialized <= True;
endrule
// output char
let outff <- mkBypassFIFOF;
rule outputChar (is_initialized);
let sent <- serv_socket_put8 (serv_socket_ptr, outff.first);
if (sent) outff.deq;
endrule
// input char
let inff <- mkBypassFIFOF;
rule inputChar (is_initialized);
let tmp <- serv_socket_get8 (serv_socket_ptr);
if (tmp != -1) inff.enq (truncate (tmp));
endrule
// interface
return tuple2 (toSink (outff), toSource(inff));
endmodule
module mkFileCharIOCore #(File inf, File outf) (CharIO);
let inff <- mkBypassFIFOF;
let outff <- mkBypassFIFOF;
rule readFromIn;
let c <- $fgetc (inf);
inff.enq (truncate (pack (c)));
endrule
rule writeToOut;
$fwrite (outf, "%c", outff.first);
outff.deq;
endrule
return tuple2 (toSink (outff), toSource (inff));
endmodule
module mkFileCharIO #(String inf, String outf) (CharIO);
let infh <- mkReg (InvalidFile);
rule init_infh (infh == InvalidFile);
File in_fh <- $fopen (inf, "r");
if (in_fh == InvalidFile) begin
$display ("cannot open %s", inf);
$finish (0);
end
infh <= in_fh;
endrule
let outfh <- mkReg (InvalidFile);
rule init_outfh (outfh == InvalidFile);
File out_fh <- $fopen (outf, "r");
if (out_fh == InvalidFile) begin
$display ("cannot open %s", outf);
$finish (0);
end
outfh <= out_fh;
endrule
let core <- mkFileCharIOCore (infh, outfh);
return core;
endmodule
module mkCharIO (CharIO);
let core <- mkFileCharIOCore (stdin, stdout);
return core;
endmodule
// AXI4Lite Slave interface
////////////////////////////////////////////////////////////////////////////////
`define PARAMS addr_sz, data_sz, 0, 0, 0, 0, 0
module mkAXI4LiteCharIOCore #(CharIO charIO) (AXI4Lite_Slave #(`PARAMS))
provisos (Add #(_a, 8, data_sz));
match {.snk, .src} = charIO;
let shim <- mkAXI4LiteShim;
let wRspFF <- mkFIFOF;
let rRspFF <- mkFIFOF;
rule doWrite;
shim.master.aw.drop;
let wflit <- get (shim.master.w);
if (wflit.wstrb[0] == 1) snk.put (truncate (wflit.wdata));
wRspFF.enq (AXI4Lite_BFlit {bresp: OKAY, buser: ?});
endrule
rule doRead;
shim.master.ar.drop;
let c <- get (src);
rRspFF.enq (AXI4Lite_RFlit {rdata: zeroExtend (c), rresp: OKAY, ruser: ?});
endrule
rule writeRsp;
shim.master.b.put (wRspFF.first);
wRspFF.deq;
endrule
rule readRsp;
shim.master.r.put (rRspFF.first);
rRspFF.deq;
endrule
return shim.slave;
endmodule
module mkAXI4LiteSocketCharIO #(String name, Integer dflt_port)
(AXI4Lite_Slave #(`PARAMS))
provisos (Add #(_a, 8, data_sz));
let charIO <- mkSocketCharIO (name, dflt_port);
let core <- mkAXI4LiteCharIOCore (charIO);
return core;
endmodule
module mkAXI4LiteFileCharIO #(String inf, String outf)
(AXI4Lite_Slave #(`PARAMS))
provisos (Add #(_a, 8, data_sz));
let charIO <- mkFileCharIO (inf, outf);
let core <- mkAXI4LiteCharIOCore (charIO);
return core;
endmodule
module mkAXI4LiteCharIO (AXI4Lite_Slave #(`PARAMS))
provisos (Add #(_a, 8, data_sz));
let charIO <- mkCharIO;
let core <- mkAXI4LiteCharIOCore (charIO);
return core;
endmodule
endpackage
`undef PARAMS