add dma peripheral
[shakti-peripherals.git] / src / peripherals / dma / GDefines.bsv
1 /*
2 Copyright (c) 2013, IIT Madras
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
6
7 * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
8 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
9 * Neither the name of IIT Madras nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
10
11 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
12 ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
13 */
14 ////////////////////////////////////////////////////////////////////////////////
15 // Copyright (c) 2010 Bluespec, Inc. ALL RIGHTS RESERVED.
16 ////////////////////////////////////////////////////////////////////////////////
17 // Filename : GDefines.bsv
18 // Description : Global type definitions
19 ////////////////////////////////////////////////////////////////////////////////
20
21 // Notes :
22
23 ////////////////////////////////////////////////////////////////////////////////
24 /// Imports
25 ////////////////////////////////////////////////////////////////////////////////
26 import Vector ::*;
27 import GetPut ::*;
28 import DefaultValue ::*;
29 import FShow ::*;
30 import FIFOF ::*;
31 import TLM2 ::*;
32 import ClientServer ::*;
33
34 `include "ARM.defines"
35
36 ////////////////////////////////////////////////////////////////////////////////
37 /// Types
38 ////////////////////////////////////////////////////////////////////////////////
39 typedef TLMAddr#(`ARM_PRM) MemAddr;
40 typedef TLMData#(`ARM_PRM) MemData;
41 typedef TLMByteEn#(`ARM_PRM) MemBen;
42
43
44
45 ////////////////////////////////////////////////////////////////////////////////
46 /// Bus Request type
47 ////////////////////////////////////////////////////////////////////////////////
48 typedef struct {
49 Bool first;
50 Bool reduced;
51 Bool inst;
52 Bool write;
53 Bool cop;
54 Bool gdb;
55 Bool c;
56 Bool b;
57 Bool usermode;
58 Bool wrap;
59 UInt#(6) id;
60 UInt#(10) burst;
61 Bit#(4) byteen;
62 Bit#(32) address;
63 MemData data;
64 } BusRequest deriving (Bits, Eq);
65
66 instance DefaultValue#(BusRequest);
67 defaultValue = BusRequest {
68 first: True,
69 reduced: False,
70 inst: False,
71 write: False,
72 cop: False,
73 gdb: False,
74 c: False,
75 b: False,
76 usermode: False,
77 wrap: False,
78 id: 0,
79 burst: 1,
80 byteen: 0,
81 address: ?,
82 data: ?
83 };
84 endinstance
85
86 ////////////////////////////////////////////////////////////////////////////////
87 /// Bus Response Type
88 ////////////////////////////////////////////////////////////////////////////////
89 typedef struct {
90 Bool error;
91 Bool cop;
92 Bool gdb;
93 Bool write;
94 UInt#(6) id;
95 MemData data;
96 } BusResponse deriving (Bits, Eq);
97
98 instance DefaultValue#(BusResponse);
99 defaultValue = BusResponse {
100 error: True,
101 cop: False,
102 gdb: False,
103 write: False,
104 id: 0,
105 data: 0
106 };
107 endinstance
108
109
110 ////////////////////////////////////////////////////////////////////////////////
111 /// Overloading Function for TLM conversions
112 ////////////////////////////////////////////////////////////////////////////////
113 instance TLMRequestTC#(BusRequest, `ARM_PRM);
114 function toTLMRequest(x);
115 if (x.first) begin
116 RequestDescriptor#(`ARM_PRM) request = createBasicRequestDescriptor;
117 request.custom[11] = pack(x.reduced);
118 request.custom[10] = pack(x.gdb);
119 request.custom[9] = pack(x.cop);
120 request.custom[8] = 0;
121 request.custom[7] = 0;
122 request.custom[6] = 0;
123 request.custom[5] = pack(x.c);
124 request.custom[4] = pack(x.c);
125 request.custom[3] = pack(x.b);
126 request.custom[2] = pack(x.inst);
127 request.custom[1] = 1;
128 request.custom[0] = pack(!x.usermode);
129 request.burst_mode = (x.wrap) ? WRAP : INCR;
130 request.transaction_id = pack(x.id);
131 request.burst_size = truncate(pack(countOnesAlt(x.byteen) - 1));
132 request.burst_length = x.burst;
133 request.byte_enable = x.byteen;
134 request.command = (x.write) ? WRITE : READ;
135 request.addr = pack(x.address);
136 request.data = x.data;
137 return (tagged Descriptor request);
138 end
139 else begin
140 RequestData#(`ARM_PRM) request = unpack(0);
141 request.custom[11] = pack(x.reduced);
142 request.custom[10] = pack(x.gdb);
143 request.custom[9] = pack(x.cop);
144 request.custom[8] = 0;
145 request.custom[7] = 0;
146 request.custom[6] = 0;
147 request.custom[5] = pack(x.c);
148 request.custom[4] = pack(x.c);
149 request.custom[3] = pack(x.b);
150 request.custom[2] = pack(x.inst);
151 request.custom[1] = 1;
152 request.custom[0] = pack(!x.usermode);
153 request.transaction_id = pack(x.id);
154 request.data = x.data;
155 return (tagged Data request);
156 end
157 endfunction
158
159 function fromTLMRequest(x);
160 case(x) matches
161 tagged Descriptor .d: begin
162 return BusRequest {
163 first: True,
164 reduced: unpack(d.custom[11]),
165 inst: unpack(d.custom[2]),
166 write: (d.command == WRITE),
167 cop: unpack(d.custom[9]),
168 gdb: unpack(d.custom[10]),
169 c: unpack(d.custom[4]),
170 b: unpack(d.custom[3]),
171 usermode: unpack(~d.custom[0]),
172 wrap: d.burst_mode == WRAP,
173 id: unpack(d.transaction_id),
174 burst: d.burst_length,
175 byteen: calculateByteEnables(d),
176 address: unpack(d.addr),
177 data: d.data
178 };
179 end
180 tagged Data .d: begin
181 return BusRequest {
182 first: False,
183 reduced: unpack(d.custom[11]),
184 inst: unpack(d.custom[2]),
185 write: ?,
186 cop: unpack(d.custom[9]),
187 gdb: unpack(d.custom[10]),
188 c: unpack(d.custom[4]),
189 b: unpack(d.custom[3]),
190 usermode: unpack(~d.custom[0]),
191 wrap: ?,
192 id: unpack(d.transaction_id),
193 burst: ?,
194 byteen: '1,
195 address: 0,
196 data: d.data
197 };
198 end
199 endcase
200 endfunction
201 endinstance
202
203 instance TLMResponseTC#(BusResponse, `ARM_PRM);
204 function toTLMResponse(x);
205 TLMResponse#(`ARM_PRM) response = unpack(0);
206 response.command = (x.write) ? WRITE : READ;
207 response.data = x.data;
208 response.status = (x.error) ? ERROR : SUCCESS;
209 response.custom[10] = pack(x.gdb);
210 response.custom[9] = pack(x.cop);
211 response.transaction_id = pack(x.id);
212 return response;
213 endfunction
214 function fromTLMResponse(x);
215 return BusResponse {
216 error: (x.status != SUCCESS),
217 gdb: unpack(x.custom[10]),
218 cop: unpack(x.custom[9]),
219 write: (x.command == WRITE),
220 id: unpack(x.transaction_id),
221 data: x.data
222 };
223 endfunction
224 endinstance
225
226 ////////////////////////////////////////////////////////////////////////////////
227 ///
228 ////////////////////////////////////////////////////////////////////////////////
229 function Bit#(4) calculateByteEnables(RequestDescriptor#(`ARM_PRM) desc);
230 let offset_mask = 3 - desc.burst_size;
231 let offset = desc.addr & zeroExtend(offset_mask);
232 let maskp1 = 1 << getTLMBurstSize(desc);
233 return (maskp1-1) << offset;
234 endfunction