233 i f ( l i n e s c a n ob j (PIXEL ADDRESS SIZE−1 downto 0) =
234 s t d l o g i c v e c t o r ( to uns igned (NUMBER OF PIXELS−1,
235 PIXEL ADDRESS SIZE) ) and l i n e s c a n ob j (
PIXEL ADDRESS SIZE ∗
236 2 − 1 downto PIXEL ADDRESS SIZE) =
s t d l o g i c v e c t o r (
237 to uns igned (0 ,PIXEL ADDRESS SIZE) ) ) then
238 s t a r t l i n e <= ’1 ’ ;
239 else
240 s t a r t l i n e <= ’0 ’ ;
241 c u r r e n t l i n e s c a n l i n e := l i n e s c a n ob j (
LINESCAN INPUT SIZE−1 downto
LINESCAN INPUT SIZE−WORD SIZE) ;
242 object num <= l i n e s c a n ob j (LINESCAN INPUT SIZE−
WORD SIZE−1 downto PIXEL ADDRESS SIZE∗2) ;
243 pixend <= l i n e s c a n ob j (PIXEL ADDRESS SIZE−1
downto 0) ;
244 p i x s t a r t <= l i n e s c a n ob j (PIXEL ADDRESS SIZE
∗2−1 downto PIXEL ADDRESS SIZE) ;
245 newlinenum <= l i n e s c a n ob j (LINESCAN INPUT SIZE−1
downto LINESCAN INPUT SIZE−WORD SIZE) ;
246
247 i f ( unsigned ( c u r r e n t l i n e s c a n l i n e ) /=
248 unsigned ( p a s t l i n e s c a n l i n e ) ) then
249 −−new l i n e
250 p r e v i o u s l i n e p i x e l s <= p i x e l t r a c k e r ;
251 p i x e l t r a c k e r <= ( others => 0) ;
114
252 end i f ;
253
254 for k in 1 to NUMBER OF PIXELS loop
255 exit when k = unsigned ( pixend ) + 1 ;
256 i f ( k >= unsigned ( p i x s t a r t ) and k <= unsigned (
pixend ) )
257 then
258 −−OBJECT NUMBER;
259 p i x e l t r a c k e r ( k ) <= to i n t e g e r ( unsigned (
object num ) ) ;
260 end i f ;
261 end loop ;
262 p a s t l i n e s c a n l i n e <= cu r r e n t l i n e s c a n l i n e ;
263 r e g r e s s l i n e <= newlinenum ;
264 end i f ;
265 end i f ;
266 end process ;
267
268
269 −−one p i x e l r e s u l t a t a time , j u s t add in as needed !
270 −−INPUT =
271 process ( data c lk , r s t n )
272 variable pixel num : i n t e g e r range 0 to
NUMBER OF PIXELS−1;
273 variable c u r r e n t l i n e : p i x e l a r r a y ;
274 variable regress f rame num : s t d l o g i c v e c t o r (WORD SIZE
−1 downto 0) ;
275 begin
276 i f ( r s t n = ’0 ’ ) then
277 new pixe l <= ( others => ’ 0 ’ ) ;
278 regress f rame num := ( others => ’ 0 ’ ) ;
279 update mem address <= ( others => ’ 0 ’ ) ;
280 output mem address <= ( others => ’ 0 ’ ) ;
281 r eady wr i t e <= ’0 ’ ;
282 e l s i f ( r i s i n g e d g e ( da ta c l k ) ) then
283 r e g s t a r t l i n e 2 <= s t a r t l i n e ;
284 r e g s t a r t l i n e <= r e g s t a r t l i n e 2 ;
285
286 r e g r e g <= r e g r e s s l i n e ;
287 r e g l a t c h <= reg r e g ;
288
289 i f ( r e g s t a r t l i n e = ’1 ’ ) then
290 regress f rame num := ( others => ’ 0 ’ ) ;
291 e l s i f ( n ew re su l t s = ’1 ’ ) then
292 pixel num := t o i n t e g e r ( unsigned ( c l a s s r e s u l t s i n
(NUMBER OF CLASSES∗WORD SIZE+
115
PIXEL ADDRESS SIZE−1 downto NUMBER OF CLASSES∗
WORD SIZE) ) ) ;
293 i f ( pixel num = 0) then
294 regress f rame num := s t d l o g i c v e c t o r (
unsigned ( regress f rame num ) + 1) ;
295 p a s t l i n e <= cu r r e n t l i n e ;
296 i f ( unsigned ( regress f rame num ) = unsigned (
r e g l a t c h ) ) then
297 c u r r e n t l i n e := p i x e l t r a c k e r ;
298 else
299 c u r r e n t l i n e := p r e v i o u s l i n e p i x e l s ;
300 end i f ;
301 end i f ;
302 end i f ;
303
304 i f ( n ew re su l t s = ’1 ’ ) then
305 i f ( pixel num > 0 and pixel num < NUMBER OF PIXELS
−1) then
306 i f ( c u r r e n t l i n e ( pixel num−1) /= 0 and
307 c u r r e n t l i n e ( pixel num ) /= 0 and
308 c u r r e n t l i n e ( pixel num+1) /= 0) then
309 −−read from memory , add toge ther , re−
wr i t e to memory
310 new pixe l <= c l a s s r e s u l t s i n (
NUMBER OF CLASSES∗
311 WORD SIZE−1 downto 0) ;
312
313 update mem address <= s t d l o g i c v e c t o r (
to uns igned
314 ( c u r r e n t l i n e ( pixel num ) ,
OBJECT ADDRESS SIZE) ) ;
315 r eady wr i t e <= ’1 ’ ;
316 e l s i f ( c u r r e n t l i n e ( pixel num−1) = 0 and
317 p a s t l i n e ( pixel num−1) /= 0) then
318
319 i f ( c u r r e n t l i n e ( pixel num ) = 0 and
320 p a s t l i n e ( pixel num ) /= 0) then
321
322 output mem address <=
s t d l o g i c v e c t o r (
323 to uns igned ( p a s t l i n e ( pixel num ) ,
324 OBJECT ADDRESS SIZE) )
;
325 end i f ;
326 r eady wr i t e <= ’0 ’ ;
327 new pixe l <= ( others => ’ 0 ’ ) ;
328 else
116
329 r eady wr i t e <= ’0 ’ ;
330 new pixe l <= ( others => ’ 0 ’ ) ;
331 end i f ;
332 else
333 r eady wr i t e <= ’0 ’ ;
334 new pixe l <= ( others => ’ 0 ’ ) ;
335 end i f ;
336 else
337 new pixe l <= ( others => ’ 0 ’ ) ;
338 r eady wr i t e <= ’0 ’ ;
339
340 end i f ;
341 new pix add <= new pixe l ;
342 r eady wr i t e2 <= ready wr i t e ;−−p i p e l i n e wh i l e adder
opera t e s
343 mem write <= ready wr i t e2 ;
344 end i f ;
345 end process ;
346
347 output proc : process ( data c lk , r s t n )
348 begin
349 i f ( r s t n = ’0 ’ ) then
350 d e c i s i o n v e c t o r <= ( others => ’ 0 ’ ) ;
351 e l s i f ( r i s i n g e d g e ( da ta c l k ) ) then
352 out object num <= output mem address ;
353 i f ( d e c i s i on ve c to r t emp /= ZEROS) then
354 d e c i s i o n v e c t o r <= out object num &
dec i s i on vec to r t emp ;
355 o b j e c t c l e a r w r i t e <= ’1 ’ ;
356 else
357 d e c i s i o n v e c t o r <= ( others => ’ 0 ’ ) ;
358 o b j e c t c l e a r w r i t e <= ’0 ’ ;
359 end i f ;
360 end i f ;
361 end process ;
362
363 end architecture ;
1 −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
2 −−
3 −−! @ f i l e DRAM controller . vhd
4 −−! @br ie f The master d r i v e r to p u l l data from DRAM.
5 −−! @de t a i l s Passes bu r s t i n g reads from DRAM through b u f f e r
6 −−! f o r use by system
7 −−! @author Monica Whitaker
8 −−! @date October 2015
9 −−! @copyright Copyright (C) 2015 Ross K. Snider and
117
10 −−! Monica Whitaker
11 −−
12 −− This program i s f r e e so f tware : you can r e d i s t r i b u t e i t and/or
13 −− modify i t under the terms o f the GNU General Pub l i c License
14 −− as pub l i s h ed by the Free Sof tware Foundation , e i t h e r ve r s i on
15 −− 3 o f the License , or ( at your opt ion ) any l a t e r ve r s i on .
16 −−
17 −− This program i s d i s t r i b u t e d in the hope t ha t i t w i l l be
18 −− use fu l , but WITHOUT ANY WARRANTY; wi thout even the imp l i ed
19 −− warranty o f MERCHANTABILITY or FITNESS FOR A PARTICULAR
20 −− PURPOSE. See the GNU General Pub l i c License f o r more d e t a i l s .
21 −−
22 −− You shou ld have r e c e i v ed a copy o f the GNU General Pub l i c
23 −− License a long wi th t h i s program . I f not , see .
24 −−
25 −− Monica Whitaker
26 −− E l e c t r i c a l and Computer Engineer ing
27 −− Montana S ta t e Un i v e r s i t y
28 −− 610 Cob le i gh Ha l l
29 −− Bozeman , MT 59717
30 −− monica . whitaker@msu . montana . edu
31 −−
32 −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
33 l ibrary IEEE ;
34 use IEEE . STD LOGIC 1164 .ALL;
35 use i e e e . numer ic std . a l l ; −−! Use numeric s tandard
36 use i e e e . math rea l . a l l ;
37
38 use work . Sensor Package .ALL;
39 −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
40 −−
41 −−! @br ie f DRAM controller
42 −−! @de t a i l s Passes bu r s t i n g reads from DRAM through b u f f e r
43 −−! f o r use by system
44 −−! @param memory clk Input r e f c l o c k
f o r DDR
45 −−! @param sy s t em c l k Buf fer data
output c l o c k
46 −−! @param r s t n Act ive low r e s e t
47 −−! @param avm read master read Master Read
enab l e
48 −−! @param avm read master address Master address
49 −−! @param avm read master burs tcount Master bur s t coun t
50 −−! @param avm read master readdata Master readdata
51 −−! @param avm read mas ter readda tava l i d Master data v a l i d
118
52 −−! @param avm read mas ter wa i t reques t Master read
wa i t r e que s t
53 −−! @param avm wr i t e mas te r wr i t e Master wr i t e
enab l e
54 −−! @param avm wri te mas ter address Master wr i t e
address
55 −−! @param avm wr i t e mas te r wr i t eda ta Master wr i t eda ta
56 −−! @param avm wr i t e mas t e r wa i t r e que s t Master wr i t e
wa i t r e que s t
57 −−! @param a v s c s r w r i t e S lave wr i t e
enab l e
58 −−! @param av s c s r a dd r e s s S lave wr i t e
address
59 −−! @param av s c s r w r i t e d a t a S lave wr i t eda ta
60 −−! @param av s c s r wa i t r e q u e s t S lave wr i t e
wa i t r e que s t
61 −−! @param w r i t e c l k Output o f
memory clk
62 −−! @param r e a d s t a r t Enable read ing
from DDR
63 −−! @param bu f f e r r e a d en Read enab l e f o r
FIFO
64 −−! @param bu f f e r empty FIFO empty
65 −−! @param bu f f e r r e a dda t a FIFO readdata
66 −−
67 −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
68 entity DRAM controller i s
69 port ( memory clk : in s t d l o g i c ;
70 sys t em c lk : in s t d l o g i c ;
71 r s t n : in s t d l o g i c ;
72
73 −−read master s i g n a l s
74 avm read master read : out s t d l o g i c ;
75 avm read master address : out s t d l o g i c v e c t o r (31
downto 0) ;
76 avm read master burstcount : out s t d l o g i c v e c t o r (5
downto 0) ;
77 avm read master readdata : in s t d l o g i c v e c t o r (127
downto 0) ;
78 avm read master readdatava l id : in s t d l o g i c ;
79 avm read master wai t request : in s t d l o g i c ;
80
81 −−wr i t e master s i g n a l s −− debug wr i t i n g s i g n a l s
82 avm wr i te master wr i t e : out s t d l o g i c ;
83 avm wri te master address : out s t d l o g i c v e c t o r (31
downto 0) ;
119
84 avm wri te master wr i tedata : out s t d l o g i c v e c t o r (127
downto 0) ;
85 avm wr i te maste r wa i t reques t : in s t d l o g i c ;
86
87 −−expor t s i g n a l s f o r wr i t i n g
88 a v s c s r w r i t e : in s t d l o g i c ;
89 av s c s r add r e s s : in s t d l o g i c v e c t o r (31
downto 0) ;
90 av s c s r w r i t e d a t a : in s t d l o g i c v e c t o r (127
downto 0) ;
91 av s c s r wa i t r e qu e s t : out s t d l o g i c ;
92 wr i t e c l k : out s t d l o g i c ;
93
94 −−condui t expor t s i g n a l s
95 r e a d s t a r t : in s t d l o g i c ; −− 1 i f wr i t e done
96 bu f f e r r e ad en : in s t d l o g i c ;
97 buf fer empty : out s t d l o g i c ;
98 bu f f e r r e adda ta : out s t d l o g i c v e c t o r (127 downto
0)
99 ) ;
100 end entity ;
101
102 architecture c o n t r o l l e r a r c h of DRAM controller i s
103 −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
104 −− Component De f i n i t i o n s
105 −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
106 component d u a l c l o c k f i f o i s
107 generic (
108 lpm numwords : natura l ;
109 lpm width : natura l ;
110 lpm widthu : natura l ;
111 rd sync de layp ipe : natura l ;
112 under f l ow check ing : s t r i n g ;
113 wrsync de layp ipe : natura l ) ;
114 port (
115 data : in s t d l o g i c v e c t o r ( lpm width − 1
downto 0)
116 := ( others => ’X’ ) ;
117 wrreq : in s t d l o g i c := ’X’ ;
118 rdreq : in s t d l o g i c := ’X’ ;
119 wrclk : in s t d l o g i c := ’X’ ;
120 rdc lk : in s t d l o g i c := ’X’ ;
121 a c l r : in s t d l o g i c := ’ 0 ’ ;
122 q : out s t d l o g i c v e c t o r ( lpm width − 1
downto 0) ;
123 rdempty : out s t d l o g i c ;
124 wr f u l l : out s t d l o g i c ;
120
125 r d f u l l : out s t d l o g i c ;
126 wrempty : out s t d l o g i c ;
127 rdusedw : out s t d l o g i c v e c t o r ( lpm widthu − 1
downto 0) ;
128 wrusedw : out s t d l o g i c v e c t o r ( lpm widthu − 1
downto 0) ;
129 e c c s t a tu s : out s t d l o g i c v e c t o r (1 downto 0) ) ;
130 end component d u a l c l o c k f i f o ;
131 −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
132 −− Constant De f i n i t i o n s
133 −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
134 constant BURST LENGTH : natura l := 32 ;
135 constant BURST LENGTH SIZE : natura l := 6 ;
136 constant BUFFERDEPTH : natura l := 1024 ;
137 constant READDATA SIZE : natura l := DRAM DATA SIZE;
138 constant TOTAL BURSTS : natura l := natura l ( trunc (
r e a l ( (NUMBER OF PIXELS∗NUMBER OF SPECTRAL BINS) /
BURST LENGTH) ) ) ;
139 constant BYTES PERWORD : natura l := natura l ( trunc (
r e a l (READDATA SIZE) / r e a l (8 ) ) ) ;
140 −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
141 −− Type De f i n i t i o n s
142 −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
143 −− s t a t e machine s t a t e s
144 type r e ad s t a t e s T i s ( i d l e ,
145 f i f o w a i t ,
146 mid burst ,
147 f i n i s h r e a d s ) ;
148 −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
149 −− S igna l Dec la ra t i ons
150 −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
151 −− f i f o s i g n a l s
152 signal bu f f e r w r i t e : s t d l o g i c ;
153 signal b u f f e r f u l l : s t d l o g i c ;
154 signal bu f f e r words : s t d l o g i c v e c t o r (9 downto 0) ;
155
156 signal r e ad s t a t e : r e ad s t a t e s T ;
157
158 −− ex t ra read master s i g n a l s
159 −− the current read address
160 signal r ead addre s s : s t d l o g i c v e c t o r (31 downto 0) ;
161 −− t r a c k s the number o f b u r s t s completed
162 signal burs t s comple ted : s t d l o g i c v e c t o r ( natura l ( trunc ( log2
( r e a l (TOTAL BURSTS) ) ) ) downto 0) ;
163 −− t r a c k s the a v a i l a b l e room in the f i f o
164 signal r o om i n f i f o : s t d l o g i c v e c t o r (10 downto 0) ;
121
165 −− t r a c k s the number o f t r an sa c t i on s t ha t are wa i t ing to be
re turned
166 signal pending reads : s t d l o g i c v e c t o r (10 downto 0) ;
167
168 −− ex t ra wr i t e master s i g n a l s
169 −− the current wr i t e address
170 signal wr i t e add r e s s : s t d l o g i c v e c t o r (31 downto 0) ;
171 −− t r ack number o f va l u e s wr i t t en
172 signal counter : i n t e g e r range 0 to TOTAL BURSTS∗
BURST LENGTH+1;
173 −− DEBUG: a l e r t read FSM when wr i t i n g complete
174 signal counter check : s t d l o g i c ;
175 signal s t a r t add r e s s 1 : s t d l o g i c v e c t o r (31 downto 0) := x"
00000000" ;
176
177 begin
178 av s c s r wa i t r e qu e s t <= avm wr i te maste r wa i t reques t ;
179 wr i t e c l k <= memory clk ;
180 avm wri te master address <= av s c s r add r e s s ;
181 avm wr i te master wr i te <= av s c s r w r i t e ;
182 avm wri te master wr i tedata <= av s c s r w r i t e d a t a ;
183
184 i d c f i f o b u f f e r : component d u a l c l o c k f i f o
185 generic map(
186 lpm numwords => BUFFER DEPTH,
187 lpm width => DRAM DATA SIZE,
188 lpm widthu => 10 ,
189 rd sync de layp ipe => 4 ,
190 under f l ow check ing => "OFF" ,
191 wrsync de layp ipe => 4
192 )
193 port map(
194 data => avm read master readdata ,
195 wrreq => bu f f e r w r i t e ,
196 rdreq => bu f f e r r e ad en ,
197 wrclk => memory clk ,
198 rdc lk => system clk ,
199 q => bu f f e r r eaddata ,
200 rdempty => buffer empty ,
201 wr f u l l => b u f f e r f u l l ,
202 a c l r => open ,
203 e c c s t a tu s => open ,
204 r d f u l l => open ,
205 rdusedw => open ,
206 wrempty => open ,
207 wrusedw => bu f f e r words
208 ) ;
122
209 −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
210 −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
211 −−READ FSM 1
212 −− read l i g h t /dark matrix va l u e s −− addres se s x ”00000000” to
x”0FFFFFFF”
213 −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
214 −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
215 read FSM 1 : process (memory clk , r s t n )
216 begin
217 i f ( r s t n = ’0 ’ or r e a d s t a r t = ’0 ’ ) then
218 r e ad s t a t e <= i d l e ;
219 r ead addre s s <= s t a r t add r e s s 1 ;
220 burs t s comple ted <= ( others => ’ 0 ’ ) ;
221 pending reads <= ( others => ’ 0 ’ ) ;
222 e l s i f ( r i s i n g e d g e (memory clk ) ) then
223
224 −− DEFAULT SECTION
225 −− decrement the pending reads counter i f data i s re turned
226 i f ( avm read master readdatava l id = ’1 ’ ) then
227 pending reads <= s t d l o g i c v e c t o r ( unsigned ( pend ing reads )
− 1) ;
228 end i f ;
229
230 case r e ad s t a t e i s
231 −− IDLE
232 −− When i d l e j u s t s i t and wai t f o r the go f l a g .
233 −− Only s t a r t i f the wr i t e s t a t e machine i s i d l e as i t
may
234 −− be f i n i s h i n g a prev ious data t r an s f e r .
235 −− S ta r t the machine by moving to the f i f o w a i t s t a t e and
236 −− i n i t i a l i s i n g address and counters .
237 when i d l e =>
238 −− i f r e a d s t a r t = ’1 ’ then
239 r e ad s t a t e <= f i f o w a i t ;
240 r ead addre s s <= s t a r t add r e s s 1 ;
241 pending reads <= ( others => ’ 0 ’ ) ;
242 burs t s comple ted <= ( others => ’ 0 ’ ) ;
243 −−end i f ;
244
245 −− FIFO WAIT
246 −− When in t h i s s t a t e wai t f o r the f i f o to have
s u f f i c i e n t
247 −− space f o r a complete bu r s t . I f so , s t a r t a
bu r s t by
248 −− moving to the mid burs t s t a t e . When moving to
mid bu r s t
123
249 −− add the bu r s t va lue to the pending reads
counter .
250 when f i f o w a i t =>
251 −− check t ha t f i f o has enough space f o r 32 word bu r s t
252 i f ( unsigned ( r o om i n f i f o ) >= BURST LENGTH + 5) then
253 r e ad s t a t e <= mid burst ;
254 −− add 32 to the pending reads counter but be
255 −− mindfu l t h a t a word may be re turned at
the same
256 −− t ime
257 i f ( avm read master readdatava l id = ’0 ’ ) then
258 pending reads <= s t d l o g i c v e c t o r ( unsigned (
pend ing reads ) + BURST LENGTH) ;
259 else
260 pending reads <= s t d l o g i c v e c t o r ( unsigned (
pend ing reads ) + BURST LENGTH−1) ;
261 end i f ;
262
263 end i f ;
264
265 −− MID BURST
266 −− Count bu r s t s
267 −− I f a l l b u r s t s complete go to f i n i s h r e a d s s t a t e .
268 −− Otherwise s tay in t h i s s t a t e i f t h e r e i s room in f i f o
or
269 −− re turn to f i f o w a i t i f not . As each bu r s t i s
completed
270 −− increment address , b u r s t s completed counter
and pending
271 −− reads counter . Be mindfu l to do noth ing i f
wa i t r e que s t
272 −− i s a c t i v e
273 when mid burst =>
274 −− i f wa i t r e que s t i s a c t i v e do nothing , o the rw i s e . . .
275 i f ( avm read master wai t request /= ’1 ’ ) then
276 i f ( burs t s comple ted = s t d l o g i c v e c t o r ( to uns igned (
TOTAL BURSTS − 1 , natura l ( trunc ( log2 ( r e a l (
TOTAL BURSTS) ) ) )+1) ) ) then
277 r e ad s t a t e <= f i n i s h r e a d s ;
278 −− no need to check f o r pending reads complete
279 −− as we ’ ve j u s t r e que s t ed another 32
words
280 else
281 burs t s comple ted <= s t d l o g i c v e c t o r ( unsigned (
burs t s comple ted ) + 1) ;
282 r ead addre s s <= s t d l o g i c v e c t o r ( unsigned (
r ead addre s s ) + BURST LENGTH∗BYTES PERWORD) ;
124
283 i f ( unsigned ( r o om i n f i f o ) >= BURST LENGTH + 5)
284 then
285 r e ad s t a t e <= mid burst ;
286 −− add 32 to the pending reads counter but
287 −− be mindfu l t h a t a word may be
re turned
288 −− at the same time
289 i f ( avm read master readdatava l id = ’0 ’ ) then
290 pending reads <= s t d l o g i c v e c t o r ( unsigned (
pend ing reads ) + BURST LENGTH) ;
291 else
292 pending reads <= s t d l o g i c v e c t o r ( unsigned (
pend ing reads ) + BURST LENGTH − 1) ;
293 end i f ;
294 else
295 r e ad s t a t e <= f i f o w a i t ;
296 end i f ;
297 end i f ;
298
299 end i f ;
300
301 −− FINISH READS
302 −− Al l the read address phases are complete but t h e r e
w i l l
303 −− be readdata pending . Jus t s i t and wai t u n t i l
t h e r e i s no
304 −− readdata pending and then move to i d l e s t a t e .
Note t ha t
305 −− the pend ing reads counter i s decremented in
the d e f a u l t
306 −− s e c t i on above .
307 when f i n i s h r e a d s =>
308 i f ( avm read master readdatava l id = ’1 ’ ) then
309 i f ( unsigned ( pend ing reads ) = 1) then
310 r e ad s t a t e <= i d l e ;
311 end i f ;
312 end i f ;
313
314 end case ;
315 end i f ;
316 end process ;
317
318 avm read master read <= ’1 ’ when r e ad s t a t e = mid burst else
’ 0 ’ ;
319
125
320 r o om i n f i f o <= s t d l o g i c v e c t o r ( r e s i z e ( ( to uns igned (
BUFFER DEPTH, natura l ( trunc ( log2 ( r e a l (BUFFERDEPTH) ) ) ) + 1)
− unsigned ( bu f f e r words ) − unsigned ( pend ing reads ) ) , 11) ) ;
321
322 avm read master address <= read addre s s ;
323
324 −− s imply wr i t e data in t o the f i f o as i t comes in ( read
a s s e r t e d and
325 −− wa i t r e que s t not a c t i v e )
326 bu f f e r w r i t e <= avm read master readdatava l id ;
327
328 avm read master burstcount <= s t d l o g i c v e c t o r ( to uns igned (
BURST LENGTH, BURST LENGTH SIZE) ) ;
329
330 −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
331 −− DEBUG sec t i on
332 −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
333 −− −− Writes counter va l u e s f o r t e s t i n g purposes .
334 −− write FSM : process (memory clk , r s t n )
335 −− beg in
336 −− i f ( r s t n = ’0 ’ ) then
337 −−wr i t e add r e s s <= s t a r t a d d r e s s 1 ;
338 −−counter check <= ’0 ’ ;
339 −−counter <= 0;
340 −−avm wr i t e mas te r wr i t e <= ’1 ’ ;
341 −− e l s i f ( r i s i n g e d g e (memory clk ) ) then
342 −− i f ( avm wr i t e mas t e r wa i t r e que s t /= ’1 ’ ) then
343
344
345 −− i f ( counter = TOTAL BURSTS∗BURST LENGTH+1) then
346 −− avm wr i t e mas te r wr i t e <= ’0 ’ ;
347 −− counter check <= ’1 ’ ;
348 −− e l s e
349 −− avm wr i t e mas t e r wr i t eda ta <=
s t d l o g i c v e c t o r (
350 −− t o uns i gned ( counter ,
READDATA SIZE) ) ;
351 −− counter <= counter + 1;
352 −− wr i t e add r e s s <= s t d l o g i c v e c t o r ( unsigned
(
353 −− wr i t e add r e s s ) +
BYTES PERWORD) ;
354 −− end i f ;
355 −− end i f ;
356 −− end i f ;
357 −− end process ;
358
126
359 −− a v s c s r wa i t r e q u e s t <= avm wr i t e mas t e r wa i t r e que s t ;
360
361 end architecture ;
1 −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
2 −−
3 −−! @ f i l e x c v r co r e . vhd
4 −−! @br ie f Transmission i n t e r f a c e
5 −−! @de t a i l s Contains t r an s c e i v e r phy and s e r i a l l i t e core f o r
6 −−! t ransmiss ion over t r an s c e i v e r s
7 −−! @author Monica Whitaker
8 −−! @date August 2016
9 −−! @copyright Copyright (C) 2016 Ross K. Snider and
10 −−! Monica Whitaker
11 −−
12 −− This program i s f r e e so f tware : you can r e d i s t r i b u t e i t and/or
13 −− modify i t under the terms o f the GNU General Pub l i c License
14 −− as pub l i s h ed by the Free Sof tware Foundation , e i t h e r ve r s i on
15 −− 3 o f the License , or ( at your opt ion ) any l a t e r ve r s i on .
16 −−
17 −− This program i s d i s t r i b u t e d in the hope t ha t i t w i l l be
18 −− use fu l , but WITHOUT ANY WARRANTY; wi thout even the imp l i ed
19 −− warranty o f MERCHANTABILITY or FITNESS FOR A PARTICULAR
20 −− PURPOSE. See the GNU General Pub l i c License f o r more d e t a i l s .
21 −−
22 −− You shou ld have r e c e i v ed a copy o f the GNU General Pub l i c
23 −− License a long wi th t h i s program . I f not , see .
24 −−
25 −− Monica Whitaker
26 −− E l e c t r i c a l and Computer Engineer ing
27 −− Montana S ta t e Un i v e r s i t y
28 −− 610 Cob le i gh Ha l l
29 −− Bozeman , MT 59717
30 −− monica . whitaker@msu . montana . edu
31 −−
32 −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
33 l ibrary IEEE ; −−! Use standard l i b r a r y .
34 use IEEE . STD LOGIC 1164 .ALL; −−! Use standard l o g i c e lements .
35 −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
36 −−
37 −−! @br ie f xcvr core
38 −−! @de t a i l s Contains t r an s c e i v e r phy and s e r i a l l i t e core f o r
39 −−! t ransmiss ion over t r an s c e i v e r s
40 −−! @param clk 100MHz Input c l k f o r phy
management
127
41 −−! @param x c v r r e f c l k Transce iver p l l r e f e r ence
c l o c k
42 −−! @param c l k da t a Clock f o r A t l an t i c
i n t e r f a c e
43 −−! @param r e s e t Act ive h igh r e s e t
44 −−! @param re s e t n Act ive low r e s e t
45 −−! @param r x s e r i a l d a t a S e r i a l r e c e i v e r i n t e r f a c e
46 −−! @param t x s e r i a l d a t a S e r i a l t ransmiss ion
i n t e r f a c e
47 −−! @param tx r eady Ready s i g n a l f o r
t ransmiss ion
48 −−! @param rx ready Ready s i g n a l f o r r e c e i v e r
49 −−! @param s t a t r r l i n k Ind i c a t e s l i n k i s up
50 −−! @param tda t Data to t ransmi t
51 −−! @param tdav Data a v a i l a b l e
52 −−! @param tena Enable t ransmiss ion
53 −−! @param tsop Transmit s t a r t o f packe t
54 −−! @param teop Transmit end o f packe t
55 −−! @param t e r r Error in t ransmi t data
56 −−! @param tmty Number o f empty by t e s in
57 −−! t ransmi t data
58 −−! @param taddr Address o f packe t to send
59 −−! @param rdav Data a v a i l a b l e
60 −−! @param r va l Data v a l i d
61 −−! @param rdat Incoming data
62 −−! @param rsop Receiver s t a r t o f packe t
s i g n a l
63 −−! @param reop Receiver end o f packe t
s i g n a l
64 −−! @param rer r Receive error
65 −−! @param rmty Number o f empty b y t e s in
66 −−! r e c e i v ed data
67 −−! @param raddr Address o f packe t
r e c e i v ed
68 −−! @param e r r r r c r c CRC error found
69 −−! @param r e c o n f i g r e s e t Reset f o r r e c on f i g u r a t i on
i n t e r f a c e
70 −−! @param re con f i g r e ad Read r e que s t
71 −−! @param r e c on f i g w r i t e Write r e que s t
72 −−! @param re con f i g a dd r e s s Recon f i gura t ion address
73 −−! @param r e c on f i g w r i t e d a t a Data to wr i t e on
74 −−! r e c on f i g u r a t i on i n t e r f a c e
75 −−! @param r e c on f i g wa i t r e q u e s t Waitrequest from
76 −−! r e c on f i g u r a t i on i n t e r f a c e
77 −−! @param recon f i g r e adda t a Data read from
78 −−! r e c on f i g u r a t i on i n t e r f a c e
79 −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
128
80 entity xcv r co r e i s
81 generic (
82 NUMBER OF LANES : natura l := 1 ;
83 LANEWIDTH : natura l := 32
84 ) ;
85 port (
86 clk 50MHz : in s t d l o g i c ;
87 x c v r r e f c l k : in s t d l o g i c ;
88 c lkdata : in s t d l o g i c ;
89 r e s e t : in s t d l o g i c ;
90 r e s e t n : in s t d l o g i c ;
91 r x s e r i a l d a t a : in s t d l o g i c ;
92 t x s e r i a l d a t a : out s t d l o g i c ;
93
94 tx ready : out s t d l o g i c ;
95 rx ready : out s t d l o g i c ;
96
97 s t a t r r l i n k : out s t d l o g i c ;
98
99 tdat : in s t d l o g i c v e c t o r ( ( (
NUMBER OF LANES ∗ LANEWIDTH)−1) downto 0) ;
100 tdav : out s t d l o g i c ;
101 tena : in s t d l o g i c ;
102 tsop : in s t d l o g i c ;
103 teop : in s t d l o g i c ;
104 t e r r : in s t d l o g i c ;
105 tmty : in s t d l o g i c v e c t o r (1 downto 0)
;
106 taddr : in s t d l o g i c v e c t o r (7 downto 0)
;
107
108 rdat : out s t d l o g i c v e c t o r ( ( (
NUMBER OF LANES ∗ LANEWIDTH)−1) downto 0) ;
109 rdav : out s t d l o g i c ;
110 r va l : out s t d l o g i c ;
111 rena : in s t d l o g i c ;
112 rsop : out s t d l o g i c ;
113 reop : out s t d l o g i c ;
114 r e r r : out s t d l o g i c ;
115 rmty : out s t d l o g i c v e c t o r (1 downto
0) ;
116 raddr : out s t d l o g i c v e c t o r (7 downto
0) ;
117
118 e r r c r c l o c k : out s t d l o g i c ;
119
120 r e c o n f i g r e s e t : in s t d l o g i c ;
129
121 r e c on f i g r e ad : in s t d l o g i c ;
122 r e c o n f i g w r i t e : in s t d l o g i c ;
123 r e c on f i g add r e s s : in s t d l o g i c v e c t o r (9 downto 0)
;
124 r e c on f i g w r i t e d a t a : in s t d l o g i c v e c t o r (31 downto
0) ;
125 r e c on f i g wa i t r e qu e s t : out s t d l o g i c ;
126 r e c on f i g r e adda t a : out s t d l o g i c v e c t o r (31 downto
0)
127 ) ;
128 end entity ;
129
130 architecture arch of xcv r co r e i s
131 −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
132 −− Component De f i n i t i o n s
133 −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
134 component a10 xcvr phy i s
135 port (
136 r x ana l o g r e s e t : in s t d l o g i c v e c t o r (0
downto 0) := ( others => ’ 0 ’ ) ;
137 r x ca l bu sy : out s t d l o g i c v e c t o r (0
downto 0) ;
138 r x c d r r e f c l k 0 : in s t d l o g i c := ’ 0 ’ ;
139 r x c l k ou t : out s t d l o g i c v e c t o r (0
downto 0) ;
140 r x c o r e c l k i n : in s t d l o g i c v e c t o r (0
downto 0) := ( others => ’ 0 ’ ) ;
141 rx datak : out s t d l o g i c v e c t o r (3
downto 0) ;
142 r x d i g i t a l r e s e t : in s t d l o g i c v e c t o r (0
downto 0) := ( others => ’ 0 ’ ) ;
143 r x d i s p e r r : out s t d l o g i c v e c t o r (3
downto 0) ;
144 r x e r r d e t e c t : out s t d l o g i c v e c t o r (3
downto 0) ;
145 r x i s l o c k e d t od a t a : out s t d l o g i c v e c t o r (0
downto 0) ;
146 r x i s l o c k e d t o r e f : out s t d l o g i c v e c t o r (0
downto 0) ;
147 r x p a r a l l e l d a t a : out s t d l o g i c v e c t o r (31
downto 0) ;
148 r x pa t t e rnde t e c t : out s t d l o g i c v e c t o r (3
downto 0) ;
149 rx runn ingd i sp : out s t d l o g i c v e c t o r (3
downto 0) ;
150 r x s e r i a l d a t a : in s t d l o g i c v e c t o r (0
downto 0) := ( others => ’ 0 ’ ) ;
130
151 r x sync s t a tu s : out s t d l o g i c v e c t o r (3
downto 0) ;
152 t x ana l o g r e s e t : in s t d l o g i c v e c t o r (0
downto 0) := ( others => ’ 0 ’ ) ;
153 t x ca l bu sy : out s t d l o g i c v e c t o r (0
downto 0) ;
154 t x c l k ou t : out s t d l o g i c v e c t o r (0
downto 0) ;
155 t x c o r e c l k i n : in s t d l o g i c v e c t o r (0
downto 0) := ( others => ’ 0 ’ ) ;
156 tx datak : in s t d l o g i c v e c t o r (3
downto 0) := ( others => ’ 0 ’ ) ;
157 t x d i g i t a l r e s e t : in s t d l o g i c v e c t o r (0
downto 0) := ( others => ’ 0 ’ ) ;
158 t x p a r a l l e l d a t a : in s t d l o g i c v e c t o r (31
downto 0) := ( others => ’ 0 ’ ) ;
159 t x s e r i a l c l k 0 : in s t d l o g i c v e c t o r (0
downto 0) := ( others => ’ 0 ’ ) ;
160 t x s e r i a l d a t a : out s t d l o g i c v e c t o r (0
downto 0) ;
161 unu s ed r x pa r a l l e l d a t a : out s t d l o g i c v e c t o r (71
downto 0) ;
162 unu s ed t x pa r a l l e l d a t a : in s t d l o g i c v e c t o r (91
downto 0) := ( others => ’ 0 ’ )
163 ) ;
164 end component a10 xcvr phy ;
165
166 component s l 2 c o r e IS
167 port (
168 r x p a r a l l e l d a t a o u t : in s t d l o g i c v e c t o r (31
downto 0) ;
169 r x c o r e c l k : in s t d l o g i c ;
170 r x c t r l d e t e c t : in s t d l o g i c v e c t o r (3
downto 0) ;
171 s t a t r r p a t t d e t : in s t d l o g i c v e c t o r (3
downto 0) ;
172 e r r r r d i s p : in s t d l o g i c v e c t o r (3
downto 0) ;
173 t x c o r e c l k : in s t d l o g i c ;
174 c t r l t c f o r c e t r a i n : in s t d l o g i c ;
175 mreset n : in s t d l o g i c ;
176 rx rdp c l k : in s t d l o g i c ;
177 rxrdp ena : in s t d l o g i c ;
178 −− r e c e i v e FIFO th r e s ho l d low − un i t s in e lements
179 c t l r x r d p f t l : in s t d l o g i c v e c t o r (7
downto 0) ;
180 c t l rx rdp eopdav : in s t d l o g i c ;
131
181 tx rdp c l k : in s t d l o g i c ;
182 txrdp ena : in s t d l o g i c ;
183 txrdp sop : in s t d l o g i c ;
184 txrdp eop : in s t d l o g i c ;
185 t x rdp e r r : in s t d l o g i c ;
186 txrdp mty : in s t d l o g i c v e c t o r (1
downto 0) ;
187 txrdp dat : in s t d l o g i c v e c t o r (31
downto 0) ;
188 txrdp adr : in s t d l o g i c v e c t o r (7
downto 0) ;
189 −− t ransmi t FIFO bu f f e r t h r e s h o l d h igh
190 c t l t x r d p f t h : in s t d l o g i c v e c t o r (7
downto 0) ;
191 f l i p p o l a r i t y : out s t d l o g i c ;
192 r r e f c l k : out s t d l o g i c ;
193 s t a t r r l i n k : out s t d l o g i c ;
194 e r r r r 8 b e r r d e t : in s t d l o g i c v e c t o r (3
downto 0) ;
195 t x p a r a l l e l d a t a i n : out s t d l o g i c v e c t o r (31
downto 0) ;
196 t x c t r l e n a b l e : out s t d l o g i c v e c t o r (3
downto 0) ;
197 t x c o r e c l o c k : out s t d l o g i c ;
198 rxrdp sop : out s t d l o g i c ;
199 rxrdp eop : out s t d l o g i c ;
200 r x rdp e r r : out s t d l o g i c ;
201 rxrdp mty : out s t d l o g i c v e c t o r (1
downto 0) ;
202 rxrdp dat : out s t d l o g i c v e c t o r (31
downto 0) ;
203 rxrdp adr : out s t d l o g i c v e c t o r (7
downto 0) ;
204 rx rdp va l : out s t d l o g i c ;
205 rxrdp dav : out s t d l o g i c ;
206 −− At l an t i c FIFO bu f f e r i s empty
207 s tat rxrdp empty : out s t d l o g i c ;
208 −− At l an t i c FIFO bu f f e r ove r f l ow and data l o s t
209 e r r t c r x r dp o f lw : out s t d l o g i c ;
210 −− At l an t i c FIFO bu f f e r ove r f l ow and data l o s t
211 e r r t x r dp o f lw : out s t d l o g i c ;
212 txrdp dav : out s t d l o g i c ;
213 −− f r equency o f f s e t t o l e r anc e FIFO bu f f e r ove r f l ow
214 −− l i n k r e s t a r t s
215 e r r r r f o f f r e o f l w : out s t d l o g i c ;
216 −− f r equency o f f s e t t o l e r anc e FIFO bu f f e r under f low
217 s t a t t c f o f f r e emp t y : out s t d l o g i c ;
132
218 −− end o f bad packe t charac t e r r e c e i v ed
219 s t a t r r e bp r x : out s t d l o g i c ;
220 −− BIP−8 error d e t e c t e d in l i n k management packe t
221 e r r r r b i p 8 : out s t d l o g i c ;
222 −− CRC error de t e c t e d
223 e r r r r c r c : out s t d l o g i c ;
224 e r r r r f c r x b n e : out s t d l o g i c ;
225 e r r r r r o e r x bn e : out s t d l o g i c ;
226 −− i n v a l i d l i n k management packe t r e c e i v ed
227 e r r r r i n v a l i d lmp r x : out s t d l o g i c ;
228 −− s t a r t o f data con t r o l word miss ing
229 e r r r r m i s s i n g s t a r t d cw : out s t d l o g i c ;
230 −− s t a r t and end address f i e l d s do not match
231 e r r r r addr mismatch : out s t d l o g i c ;
232 −− p o s s i b l e c a t a s t r o ph i c error
233 e r r r r p o l r e v r e q u i r e d : out s t d l o g i c
234 ) ;
235 end component ;
236
237 component d u a l c l o c k f i f o i s
238 generic (
239 enab l e e c c : s t r i n g := "FALSE" ;
240 i n t ended dev i c e f am i l y : s t r i n g := "Arria 10" ;
241 lpm hint : s t r i n g
242 := "
DISABLE_DCFIFO_EMBEDDED_TIMING_CONSTRAINT
=TRUE" ;
243 lpm numwords : natura l ;
244 lpm showahead : s t r i n g := "OFF" ;
245 lpm type : s t r i n g := "dcfifo" ;
246 lpm width : natura l ;
247 lpm widthu : natura l ;
248 ove r f l ow check ing : s t r i n g := "ON" ;
249 rd sync de layp ipe : natura l ;
250 under f l ow check ing : s t r i n g := "ON" ;
251 use eab : s t r i n g := "ON" ;
252 wrsync de layp ipe : natura l
253 ) ;
254 port (
255 data : in s t d l o g i c v e c t o r ( lpm width − 1
downto 0) := ( others => ’X’ ) ;
256 wrreq : in s t d l o g i c := ’X’ ;
257 rdreq : in s t d l o g i c := ’X’ ;
258 wrclk : in s t d l o g i c := ’X’ ;
259 rdc lk : in s t d l o g i c := ’X’ ;
260 a c l r : in s t d l o g i c := ’ 0 ’ ;
133
261 q : out s t d l o g i c v e c t o r ( lpm width − 1
downto 0) ;
262 rdempty : out s t d l o g i c ;
263 wr f u l l : out s t d l o g i c ;
264 r d f u l l : out s t d l o g i c ;
265 wrempty : out s t d l o g i c ;
266 rdusedw : out s t d l o g i c v e c t o r ( lpm widthu − 1
downto 0) ;
267 wrusedw : out s t d l o g i c v e c t o r ( lpm widthu − 1
downto 0) ;
268 e c c s t a tu s : out s t d l o g i c v e c t o r (1 downto 0)
269 ) ;
270 end component ;
271
272 component x c v r p l l i s
273 port (
274 p l l c a l b u s y : out s t d l o g i c ;
275 p l l l o c k e d : out s t d l o g i c ;
276 pll powerdown : in s t d l o g i c := ’ 0 ’ ;
277 p l l r e f c l k 0 : in s t d l o g i c := ’ 0 ’ ;
278 t x s e r i a l c l k : out s t d l o g i c
279 ) ;
280 end component ;
281
282 component x c v r r e s e t i s
283 port (
284 c l o ck : in s t d l o g i c := ’ 0 ’ ;
285 p l l l o c k e d : in s t d l o g i c v e c t o r (0 downto 0)
:= ( others => ’ 0 ’ ) ;
286 pll powerdown : out s t d l o g i c v e c t o r (0 downto 0)
;
287 p l l s e l e c t : in s t d l o g i c v e c t o r (0 downto 0)
:= ( others => ’ 0 ’ ) ;
288 r e s e t : in s t d l o g i c := ’ 0 ’ ;
289 r x ana l o g r e s e t : out s t d l o g i c v e c t o r (0 downto 0)
;
290 r x ca l bu sy : in s t d l o g i c v e c t o r (0 downto 0)
:= ( others => ’ 0 ’ ) ;
291 r x d i g i t a l r e s e t : out s t d l o g i c v e c t o r (0 downto 0)
;
292 r x i s l o c k e d t od a t a : in s t d l o g i c v e c t o r (0 downto 0)
:= ( others => ’ 0 ’ ) ;
293 rx ready : out s t d l o g i c v e c t o r (0 downto 0)
;
294 t x ana l o g r e s e t : out s t d l o g i c v e c t o r (0 downto 0)
;
134
295 t x ca l bu sy : in s t d l o g i c v e c t o r (0 downto 0)
:= ( others => ’ 0 ’ ) ;
296 t x d i g i t a l r e s e t : out s t d l o g i c v e c t o r (0 downto 0)
;
297 tx ready : out s t d l o g i c v e c t o r (0 downto 0)
298 ) ;
299 end component ;
300
301 −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
302 −− S igna l De f i n i t i o n s
303 −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
304 signal ONES : s t d l o g i c v e c t o r (
NUMBER OF LANES−1 downto 0) ;
305
306 signal r x f r e q l o c k ed : s t d l o g i c v e c t o r (
NUMBER OF LANES−1 downto 0) ;
307
308 signal c t l r x r d p f t l : s t d l o g i c v e c t o r (7 downto
0) ;
309 signal c t l t x r d p f t h : s t d l o g i c v e c t o r (7 downto
0) ;
310 signal s t a t r r l i n k m i n 2 : s t d l o g i c ;
311 signal s t a t r r l i n k m i n 1 : s t d l o g i c ;
312
313 signal s tat rxrdp empty : s t d l o g i c ;
314 signal e r r t c r x r dp o f lw : s t d l o g i c ;
315 signal e r r t x r dp o f lw : s t d l o g i c ;
316 signal e r r r r f o f f r e o f l w : s t d l o g i c ;
317 signal s t a t t c f o f f r e emp t y : s t d l o g i c ;
318 signal s t a t r r e bp r x : s t d l o g i c ;
319 signal e r r r r b i p 8 : s t d l o g i c ;
320 signal e r r r r f c r x b n e : s t d l o g i c ;
321 signal e r r r r r o e r x bn e : s t d l o g i c ;
322 signal e r r r r i n v a l i d lmp r x : s t d l o g i c ;
323 signal e r r r r m i s s i n g s t a r t d cw : s t d l o g i c ;
324 signal e r r r r addr mismatch : s t d l o g i c ;
325 signal e r r r r c r c : s t d l o g i c ;
326
327 signal r x p a r a l l e l d a t a : s t d l o g i c v e c t o r ( (
NUMBER OF LANES ∗ LANEWIDTH)−1 downto 0) ;
328 signal t x p a r a l l e l d a t a : s t d l o g i c v e c t o r ( (
NUMBER OF LANES ∗ LANEWIDTH)−1 downto 0) ;
329 signal tx datak : s t d l o g i c v e c t o r (3 downto
0) ;
330 signal rx datak : s t d l o g i c v e c t o r (3 downto
0) ;
331
135
332 signal r x c o r e c l k : s t d l o g i c v e c t o r (
NUMBER OF LANES − 1 downto 0) ;
333 signal t x c o r e c l k : s t d l o g i c v e c t o r (
NUMBER OF LANES − 1 downto 0) ;
334 signal r x c l k ou t : s t d l o g i c v e c t o r (
NUMBER OF LANES − 1 downto 0) ;
335 signal t x c l k ou t : s t d l o g i c v e c t o r (
NUMBER OF LANES − 1 downto 0) ;
336 signal t x c o r e c l o c k : s t d l o g i c ;
337 signal r r e f c l k : s t d l o g i c ;
338
339 signal r x d i s p e r r : s t d l o g i c v e c t o r (3 downto
0) ;
340 signal r x e r r d e t e c t : s t d l o g i c v e c t o r (3 downto
0) ;
341 signal r x pa t t e rnde t e c t : s t d l o g i c v e c t o r (3 downto
0) ;
342
343 signal tx ca l busy combined : s t d l o g i c v e c t o r (0 downto
0) ;
344 signal t x s e r i a l c l k p l l : s t d l o g i c ;
345 signal pll powerdown : s t d l o g i c ;
346 signal p l l c a l b u s y : s t d l o g i c ;
347 signal p l l l o c k e d : s t d l o g i c ;
348 signal t x s e r i a l c l k : s t d l o g i c v e c t o r (
NUMBER OF LANES−1 downto 0) ;
349
350 signal t x ca l bu sy : s t d l o g i c v e c t o r (0 downto
0) ;
351 signal t x r e ady i : s t d l o g i c v e c t o r (0 downto
0) ;
352 signal r x ca l bu sy : s t d l o g i c v e c t o r (0 downto
0) ;
353 signal r x r e ady i : s t d l o g i c v e c t o r (0 downto
0) ;
354 signal r x a n a l o g r e s e t i : s t d l o g i c v e c t o r (0 downto
0) ;
355 signal r x d i g i t a l r e s e t i : s t d l o g i c v e c t o r (0 downto
0) ;
356 signal t x a n a l o g r e s e t i : s t d l o g i c v e c t o r (0 downto
0) ;
357 signal t x d i g i t a l r e s e t i : s t d l o g i c v e c t o r (0 downto
0) ;
358
359
360 signal w req : s t d l o g i c ;
361 signal r r e q : s t d l o g i c ;
136
362 signal w fu l l : s t d l o g i c ;
363 signal r empty : s t d l o g i c ;
364 signal e r r 8 b l o c k : s t d l o g i c ;
365 signal e r r addr mismatch lock : s t d l o g i c ;
366 signal e r r b i p 8 l o c k : s t d l o g i c ;
367 signal e r r i n v a l i d lmp r x l o c k : s t d l o g i c ;
368 signal e r r m i s s i n g l o c k : s t d l o g i c ;
369 signal e r r a r r a y : s t d l o g i c v e c t o r (4 downto
0) ;
370
371 begin
372
373 generate ALTGX clocks :
374 for i in 0 to NUMBER OF LANES−1 generate
375 r x c o r e c l k ( i ) <= rx c l kou t (0 ) ;
376 t x c o r e c l k ( i ) <= tx c l kou t (0 ) ;
377 tx ca l busy combined ( i ) <= tx ca l bu sy ( i ) or p l l c a l b u s y
;
378 end generate ;
379
380 g e n e r a t e x c v r s e r i a l c l o c k s 1 :
381 for i in 0 to NUMBER OF LANES−1 generate
382 t x s e r i a l c l k ( i ) <= t x s e r i a l c l k p l l ;
383 end generate ;
384
385 u0 : component a10 xcvr phy
386 port map(
387 r x ana l o g r e s e t => r x an a l o g r e s e t i ,
388 r x ca l bu sy => rx ca l busy ,
389 r x c d r r e f c l k 0 => x cv r r e f c l k ,
390 r x c l k ou t => rx c lkout ,
391 r x c o r e c l k i n => r x co r e c l k ,
392 rx datak => rx datak ,
393 r x d i g i t a l r e s e t => r x d i g i t a l r e s e t i ,
394 r x d i s p e r r => r x d i sp e r r ,
395 r x e r r d e t e c t => r x e r r d e t e c t ,
396 r x i s l o c k e d t od a t a => r x f r eq l o ck ed ,
397 r x i s l o c k e d t o r e f => open ,
398 r x p a r a l l e l d a t a => r x p a r a l l e l d a t a ,
399 rx runn ingd i sp => open ,
400 r x pa t t e rnde t e c t => rx pa t t e rnde t e c t ,
401 r x s e r i a l d a t a (0 ) => r x s e r i a l d a t a ,
402 r x sync s t a tu s => open ,
403 t x ana l o g r e s e t => t x an a l o g r e s e t i ,
404 t x ca l bu sy => tx ca l busy ,
405 t x c l k ou t => tx c lkout ,
406 t x c o r e c l k i n => t x co r e c l k ,
137
407 tx datak => tx datak ,
408 t x d i g i t a l r e s e t => t x d i g i t a l r e s e t i ,
409 t x p a r a l l e l d a t a => t x p a r a l l e l d a t a ,
410 t x s e r i a l c l k 0 => t x s e r i a l c l k ,
411 t x s e r i a l d a t a (0 ) => t x s e r i a l d a t a ,
412 unu s ed r x pa r a l l e l d a t a => open ,
413 unu s ed t x pa r a l l e l d a t a => ( others => ’ 0 ’ )
414 ) ;
415
416 u1 : s l 2 c o r e
417 port map(
418 r x p a r a l l e l d a t a o u t => r x p a r a l l e l d a t a ,
419 r x c o r e c l k => r x c o r e c l k (0 ) ,
420 r x c t r l d e t e c t => rx datak ,
421 s t a t r r p a t t d e t => rx pa t t e rnde t e c t ,
422 e r r r r d i s p => r x d i sp e r r ,
423 t x c o r e c l k => t x c o r e c l k (0 ) ,
424 c t r l t c f o r c e t r a i n => ’ 0 ’ ,
425 mreset n => r e s e t n ,
426 rx rdp c l k => c lkdata ,
427 rxrdp ena => rena ,
428 c t l r x r d p f t l => c t l r x r d p f t l ,
429 c t l rx rdp eopdav => ’ 0 ’ ,
430 tx rdp c l k => c lkdata ,
431 txrdp ena => tena ,
432 txrdp sop => tsop ,
433 txrdp eop => teop ,
434 t x rdp e r r => t e r r ,
435 txrdp mty => tmty ,
436 txrdp dat => tdat ,
437 txrdp adr => taddr ,
438 c t l t x r d p f t h => c t l t x r dp f t h ,
439 f l i p p o l a r i t y => open ,
440 r r e f c l k => r r e f c l k ,
441 s t a t r r l i n k => s t a t r r l i n k m in2 ,
442 e r r r r 8 b e r r d e t => r x e r r d e t e c t ,
443 t x p a r a l l e l d a t a i n => t x p a r a l l e l d a t a ,
444 t x c t r l e n a b l e => tx datak ,
445 t x c o r e c l o c k => t x co r e c l o ck ,
446 rxrdp sop => rsop ,
447 rxrdp eop => reop ,
448 r x rdp e r r => r e r r ,
449 rxrdp mty => rmty ,
450 rxrdp dat => rdat ,
451 rxrdp adr => raddr ,
452 rx rdp va l => rva l ,
453 rxrdp dav => rdav ,
138
454 s tat rxrdp empty => stat rxrdp empty ,
455 e r r t c r x r dp o f lw => e r r t c r x r dp o f lw ,
456 e r r t x r dp o f lw => e r r t x rdp o f lw ,
457 txrdp dav => tdav ,
458 e r r r r f o f f r e o f l w => e r r r r f o f f r e o f l w ,
459 s t a t t c f o f f r e emp t y => s t a t t c f o f f r e emp t y ,
460 s t a t r r e bp r x => s t a t r r ebp rx ,
461 e r r r r b i p 8 => e r r r r b i p 8 ,
462 e r r r r c r c => e r r r r c r c ,
463 e r r r r f c r x b n e => e r r r r f c r x bn e ,
464 e r r r r r o e r x bn e => e r r r r r o e r x bn e ,
465 e r r r r i n v a l i d lmp r x => e r r r r i n v a l i d lmp r x ,
466 e r r r r m i s s i n g s t a r t d cw => e r r r r m i s s i n g s t a r t d cw ,
467 e r r r r addr mismatch => er r r r addr mismatch ,
468 e r r r r p o l r e v r e q u i r e d => open
469 ) ;
470
471 u2 : x c v r p l l
472 port map(
473 p l l c a l b u s y => p l l c a l bu s y ,
474 p l l l o c k e d => p l l l o c k ed ,
475 pll powerdown => pll powerdown ,
476 p l l r e f c l k 0 => x cv r r e f c l k ,
477 t x s e r i a l c l k => t x s e r i a l c l k p l l
478 ) ;
479
480 u3 : x c v r r e s e t
481 port map(
482 c l o ck => clk 50MHz ,
483 p l l l o c k e d (0 ) => p l l l o c k ed ,
484 pll powerdown (0) => pll powerdown ,
485 p l l s e l e c t => ( others => ’ 0 ’ ) ,
486 r e s e t => r e s e t ,
487 r x ana l o g r e s e t => r x an a l o g r e s e t i ,
488 r x ca l bu sy => rx ca l busy ,
489 r x d i g i t a l r e s e t => r x d i g i t a l r e s e t i ,
490 r x i s l o c k e d t od a t a => r x f r eq l o ck ed ,
491 rx ready => r x r eady i ,
492 t x ana l o g r e s e t => t x an a l o g r e s e t i ,
493 t x ca l bu sy => tx ca l busy combined ,
494 t x d i g i t a l r e s e t => t x d i g i t a l r e s e t i ,
495 tx ready => t x r e ady i
496 ) ;
497
498 f i f o l o c k : d u a l c l o c k f i f o
499 generic map(
500 lpm numwords => 32 ,
139
501 lpm width => 5 ,
502 lpm widthu => 5 ,
503 rd sync de layp ipe => 3 ,
504 wrsync de layp ipe => 3
505 )
506 port map(
507 data => e r r r r b i p 8 & e r r r r c r c &
e r r r r i n v a l i d lmp r x &
508 e r r r r m i s s i n g s t a r t d cw &
err r r addr mismatch ,
509 wrreq => w req ,
510 rdreq => r r eq ,
511 wrclk => r r e f c l k ,
512 rdc lk => c lkdata ,
513 a c l r => ’ 0 ’ ,
514 q => e r r a r ray ,
515 rdempty => r empty ,
516 wr f u l l => w fu l l ,
517 r d f u l l => open ,
518 wrempty => open
519 ) ;
520 −−Ava i l a b l e f o r f u t u r e cons i d e ra t i on
521 e r r b i p 8 l o c k <= er r a r r a y (4 ) ;
522 e r r c r c l o c k <= er r a r r a y (3 ) ;
523 e r r i n v a l i d lmp r x l o c k <= er r a r r a y (2 ) ;
524 e r r m i s s i n g l o c k <= er r a r r a y (1 ) ;
525 e r r addr mismatch lock <= er r a r r a y (0 ) ;
526
527 process ( r r e f c l k )
528 begin
529 i f ( r i s i n g e d g e ( r r e f c l k ) ) then
530 i f ( w f u l l = ’0 ’ ) then
531 w req <= ’1 ’ ;
532 else
533 w req <= ’0 ’ ;
534 end i f ;
535 end i f ;
536 end process ;
537
538 process ( c lkdata )
539 begin
540 i f ( r i s i n g e d g e ( c lkdata ) ) then
541 i f ( r empty = ’0 ’ ) then
542 r r e q <= ’1 ’ ;
543 else
544 r r e q <= ’0 ’ ;
545 end i f ;
140
546 end i f ;
547 end process ;
548
549
550 −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
551 −− Generate Zeroes and Ones
552 −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
553 generate ZEROES and ONES :
554 for i in 0 to NUMBER OF LANES−1 generate
555 ONES( I ) <= ’1 ’ ;
556 end generate ;
557
558 −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
559 −− Generate t x r eady and rx ready
560 −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
561 tx ready <= ’1 ’ when t x r e ady i = ONES else ’ 0 ’ ;
562 rx ready <= ’1 ’ when r x r e ady i = ONES else ’ 0 ’ ;
563
564 c t l r x r d p f t l <= "00010010" ; −− Set a r b i t r a r i l y ( check
s imu la t i on )
565 c t l t x r d p f t h <= "01110000" ; −− Set a r b i t r a r i l y ( check
s imu la t i on )
566
567 −−r e g i s t e r f o r l i n k s t a t u s
568 process ( clk 50MHz , r e s e t )
569 begin
570 i f ( r e s e t = ’1 ’ ) then
571 s t a t r r l i n k m i n 1 <= ’0 ’ ;
572 s t a t r r l i n k <= ’0 ’ ;
573 e l s i f ( r i s i n g e d g e ( clk 50MHz ) ) then
574 s t a t r r l i n k m i n 1 <= s t a t r r l i n k m i n 2 ;
575 s t a t r r l i n k <= s t a t r r l i n k m i n 1 ;
576 end i f ;
577 end process ;
578 end architecture ;
141
APPENDIX C
MATLAB CODE
142
1 %
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
2 % s o r t t b .m
3 % Testbench f o r s o r t i n g component −− s o r t in two c y c l e s
4 %
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
5 clc ;
6
7 s o r t hd l = hdlcos im top ;
8 NUM TRIALS = 50 ;
9
10 for k = 1 :NUM TRIALS
11 %Bui ld a l l the random inpu t s
12 in1 = s i n g l e (randn ( ) ) ;
13 in2 = s i n g l e (randn ( ) ) ;
14 in3 = s i n g l e (randn ( ) ) ;
15 in4 = s i n g l e (randn ( ) ) ;
16 in5 = s i n g l e (randn ( ) ) ;
17 in6 = s i n g l e (randn ( ) ) ;
18 in7 = s i n g l e (randn ( ) ) ;
19 in8 = s i n g l e (randn ( ) ) ;
20 in9 = s i n g l e (randn ( ) ) ;
21 in10 = s i n g l e (randn ( ) ) ;
22 in11 = s i n g l e (randn ( ) ) ;
23 in12 = s i n g l e (randn ( ) ) ;
24 in13 = s i n g l e (randn ( ) ) ;
25 in14 = s i n g l e (randn ( ) ) ;
26 in15 = s i n g l e (randn ( ) ) ;
27 in16 = s i n g l e (randn ( ) ) ;
28 in17 = s i n g l e (randn ( ) ) ;
29 in18 = s i n g l e (randn ( ) ) ;
30 in19 = s i n g l e (randn ( ) ) ;
31 in20 = s i n g l e (randn ( ) ) ;
32
33 i n pu t h i s t o r y {k} = [ in20 in19 in18 in17 in16 in15 in14 in13
in12 in11 in10 in9 in8 in7 in6 in5 in4 in3 in2 in1 ] ;
34
35 %input in t o system
36 [ out20 out19 out18 out17 out16 out15 out14 out13 out12 out11
out10 out9 out8 out7 out6 out5 out4 out3 out2 out1 . . .
37 ind1 ind2 ind3 ind4 ind5 ind6 ind7 ind8 ind9 ind10 ind11
ind12 ind13 ind14 ind15 ind16 ind17 ind18 ind19 ind20 ]
= . . .
143
38 s tep ( s o r t hd l , in1 , in2 , in3 , in4 , in5 , in6 , in7 , in8 , in9 , in10
, in11 , in12 , in13 , in14 , in15 , in16 , in17 , in18 , in19 , in20 )
;
39
40 ou tput h i s t o ry {k} = [ out20 out19 out18 out17 out16 out15
out14 out13 out12 out11 out10 out9 out8 out7 out6 out5
out4 out3 out2 out1 ] ;
41 ou tpu t i nd i c e s {k} = [ ind1 ind2 ind3 ind4 ind5 ind6 ind7 ind8
ind9 ind10 ind11 ind12 ind13 ind14 ind15 ind16 ind17 ind18
ind19 ind20 ] ;
42 end ;
43
44 l a t ency = 2 ;
45 for k = 1 :NUM TRIALS−l a t ency
46 o r i g i n a l = i npu t h i s t o r y {k}
47 % sor t ed = ou t p u t h i s t o r y {k+l a t ency }
48 so r t ed (k , : ) = output h i s t o ry {k+la t ency }
49 temp = outpu t i nd i c e s {k+la t ency } ;
50 % ind i c e s = doub le ( temp )
51 i n d i c e s (k , : ) = double ( temp) ;
52
53 %compute s o r t in MATLAB
54 [ a c tua l (k , : ) , a c tua l i ndex (k , : ) ] = sort ( o r i g i n a l ) ;
55
56 v a l d i f f = actual−so r t ed ;
57
58 i n d d i f f = actua l index−i n d i c e s ;
59 end ;
60
61 T = tab l e ( sorted , a c tua l ) ;
62 wr i t e t ab l e (T, ’sorted.xlsx’ ,’Range’ ,’B1’ ) ;
63 T = tab l e ( i nd i c e s , a c tua l i ndex ) ;
64 wr i t e t ab l e (T, ’indices.xlsx’ ,’Range’ ,’B1’ ) ;
65 T = tab l e ( v a l d i f f , i n d d i f f ) ;
66 wr i t e t ab l e (T, ’errors.xlsx’ ,’Range’ ,’B1’ ) ;
1 %
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
2 % camera ra t i o s t b .m
3 % Testbench f o r v e r i f i c a t i o n o f co r r e c t r a t i o c a l c u l a t i o n s
4 %
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
5 clc ;
6
7 s o r t hd l = hd l co s im camera r e l a t i on s ;
144
8 l i n e s = 10 ;%648;
9 hyp e r l i n e s = 160 ;
10 l i n e s c a n p i x e l s = 8000 ; %1536;
11 hype r p i x e l s = 1024 ; %200;
12 p i x e l r a t i o = l i n e s c a n p i x e l s / hype r p i x e l s ; %7.8125
13 l i n e r a t i o = l i n e s / hyp e r l i n e s ; %4.05
14
15 o f f s e t = f i ( 32 , 1 , 13 , 0 ) ;
16 p i x r a t i o = f i ( (1/ p i x e l r a t i o ) , 0 , 32 ,32 ) ;
17 l i n e r a t = f i ( (1/ l i n e r a t i o ) , 0 , 32 ,32 ) ;
18 s tep ( s o r t hd l , o f f s e t , p i x r a t i o , l i n e r a t , f i ( 0 , 0 , 32 , 0 ) , f i ( 0 , 0 , 13 , 0 )
, f i ( 0 , 0 , 13 , 0 ) ) ;
19
20 for k = 1 : l i n e s
21 for j = 0 : l i n e s c a n p i x e l s −1
22 l ine = f i (k , 0 , 3 2 , 0 ) ;
23 s t a r t = f i ( j , 0 , 1 3 , 0 ) ;
24 end pix = f i ( j , 0 , 1 3 , 0 ) ;
25
26 i n pu t h i s t o r y { j+1} = [ l ine s t a r t end pix ] ;
27 [ r e g l i n e , r e g s t a r t , r eg endp ix i gnore ] = step ( s o r t hd l ,
o f f s e t , p i x r a t i o , l i n e r a t , l ine , s t a r t , end pix ) ;
28 ou tput h i s t o ry { j+1} = [ r e g l i n e r e g s t a r t reg endp ix ] ;
29 ou tpu t f l a g { j+1} = ignore ;
30 end ;
31 end ;
32
33 e r r o r s = 0 ;
34 sim = zeros ( l i n e s c a n p i x e l s , 2 ) ;
35 ac tua l = zeros ( l i n e s c a n p i x e l s , 2 ) ;
36 l a t ency = 1 ;
37 for k = 1 : l i n e s
38 for j = 0 : l i n e s c a n p i x e l s −1−l a t ency
39 o r i g i n a l = i npu t h i s t o r y { j +1};
40 computed = output h i s t o ry { j+1+la t ency } ;
41 inp = f i ( o r i g i n a l (2 ) , 0 , 13 , 0 ) ;
42 comp = f i ( computed (2 ) , 0 , 10 , 0 ) ;
43
44 i g n o r e f l a g ( j +1) = outpu t f l a g { j+1+la t ency } ;
45
46 sim ( j +1 , : ) = [ inp comp ] ;
47 l i n e p i x = f i ( j , 0 , 1 3 , 0 ) ;
48 act = f loor ( ( l i n e p i x+o f f s e t ) ∗ p i x r a t i o ) ;
49 ac tua l ( j +1 , : ) = [ l i n e p i x act ] ;
50
51 i f act ˜= comp
52 e r r o r s = e r r o r s + 1 ;
145
53 end ;
54 end ;
55 end ;
56
57 plot ( sim ( : , 1 ) ’ , sim ( : , 2 ) ’ , ’r’ ) ;
58 hold on ;
59 plot ( ac tua l ( : , 1 ) ’ , a c tua l ( : , 2 ) ’ , ’*’ ) ;
60
61 save t e s t
62 clear
63 load t e s t
1 %
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
2 % o b j e c t s t b .m
3 % Testbench f o r c l a s s i f i c a t i o n o f o b j e c t s . U t i l i z e s two o b j e c t s .
4 %
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
5 i f (˜ exist (’class_data’ ,’var’ ) )
6 [ Img , s t a r t p i x , end pix ] = edge bu i l d e r (’objects_simple.png’ ) ;
7 for k = 1:50
8 data ( : , : , k ) = x l s r ead (’results_luckycharm.xlsx’ , k+20,’B2:
F65’ ) ;
9 end ;
10 c l a s s d a t a = s i n g l e ( data ) ;
11 end ;
12
13 clc ;
14 c l a s s e s = 5 ;
15 l i n e s = 192∗10;%648;
16 hyp e r l i n e s = 10 ;
17 l i n e s c a n p i x e l s = 1536 ;
18 hype r p i x e l s = 64 ;
19 p i x e l r a t i o = l i n e s c a n p i x e l s / hype r p i x e l s ; %24
20 l i n e r a t i o = l i n e s / hyp e r l i n e s ; %192
21 num objects = 2 ;
22
23 ob j e c t s hd l = hdlcos im top ; % Set up s imu la t i on o b j e c t
24
25 ob j e c t = f i ( 0 , 0 , 54 , 0 ) ;
26 o f f s e t = f i ( 0 , 1 , 13 , 0 ) ;
27 p i x r a t i o = f i ( (1/ p i x e l r a t i o ) , 0 , 32 ,32 ) ;
28 l i n e r a t = f i ( (1/ l i n e r a t i o ) , 0 , 32 ,32 ) ;
29
30 f r ame f l a g = f i ( 1 , 0 , 1 , 0 ) ;
146
31 pixel num = f i ( 0 , 0 , 10 , 0 ) ;
32 c u r r e n t p i x e l = 0 ;
33 da ta t r a cke r = 1 ;
34 new = 0 ;
35 in1 = f i ( 0 , 0 , 32 , 0 ) ;
36 in2 = f i ( 0 , 0 , 32 , 0 ) ;
37 in3 = f i ( 0 , 0 , 32 , 0 ) ;
38 in4 = f i ( 0 , 0 , 32 , 0 ) ;
39 in5 = f i ( 0 , 0 , 32 , 0 ) ;
40 c u r r e n t o b j l i n e = 345 ;%271;
41 for K = 0 : hyp e r l i n e s %10
42 for M = 0: hype r p i x e l s %64
43 i f ( c u r r e n t p i x e l == 64)
44 da ta t r a cke r = data t r a cke r + 1 ;
45 c u r r e n t p i x e l = 0 ;
46 c u r r e n t o b j l i n e = c u r r e n t o b j l i n e + 1 ;
47 end ;
48 pixel num = f i ( cu r r en t p i x e l , 0 , 8 , 0 ) ;
49 in1 . hex = num2hex( c l a s s d a t a ( c u r r e n t p i x e l +1 ,1 ,
da ta t r a cke r ) ) ;
50 in2 . hex = num2hex( c l a s s d a t a ( c u r r e n t p i x e l +1 ,2 ,
da ta t r a cke r ) ) ;
51 in3 . hex = num2hex( c l a s s d a t a ( c u r r e n t p i x e l +1 ,3 ,
da ta t r a cke r ) ) ;
52 in4 . hex = num2hex( c l a s s d a t a ( c u r r e n t p i x e l +1 ,4 ,
da ta t r a cke r ) ) ;
53 in5 . hex = num2hex( c l a s s d a t a ( c u r r e n t p i x e l +1 ,5 ,
da ta t r a cke r ) ) ;
54 c u r r e n t p i x e l = cu r r e n t p i x e l + 1 ;
55
56 new re su l t s = f i ( 1 , 0 , 1 , 0 ) ;
57 for J = 1 : ( l i n e r a t i o / hype r p i x e l s )%5 l i n e s
58 for X = 1 : num objects
59 i f J ˜= 1 | | X ˜= 1
60 new re su l t s = f i ( 0 , 0 , 1 , 0 ) ;
61 end ;
62 ob j e c t = b i t conca t ( f i (K, 0 , 3 2 , 0 ) , f i (X, 0 , 6 , 0 ) , f i (
f loor ( p i x r a t i o ∗ s t a r t p i x ( c u r r e n t o b j l i n e ,X) )
, 0 , 8 , 0 ) , f i ( f loor ( p i x r a t i o ∗ end pix (
c u r r e n t o b j l i n e ,X) ) , 0 , 8 , 0 ) ) ;
63 % Run data in t o system
64 [ out1 , out2 , out3 , out4 , out5 , objectnum ] = step (
ob j e c t s hd l , ob ject , new resu l t s , pixel num , in1
, in2 , in3 , in4 , in5 ) ;
65 end ;
66 end ;
67 end ;
147
68 end ;
69
70 save t e s t . mat
71 clear ;
72 load t e s t . mat
1 %
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
2 % norma l i z e t b .m
3 % Testbench f o r normal ize component
4 %
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
5 % Sta r t i n i t i a l i z a t i o n
6 i f (˜ exist (’data’ , ’var’ ) )
7 load (’data.mat’ ) ;
8 end ;
9
10 clc ;
11 i t e r a t i o n s = 1 ;
12 rows = 64 ;
13 columns = 64 ;
14 product hd l = hdlcos im top ; % Set up s imu la t i on o b j e c t
15
16 for K = 1 : i t e r a t i o n s
17 for J = 0 : columns − 1
18 for I = 0 : rows − 1
19 data in = data ( I+1, J+1) ;
20 darkin = dark ( I+1, J+1) ;
21 l i g h t i n = l i g h t I ( I+1, J+1) ;
22 meanin = means (1 , J+1) ;
23 s tddev in = stddevI (1 , J+1) ;
24 i n pu t h i s t o r y { I+1,J+1} = [ datain , darkin , l i g h t i n ,
meanin , s tddev in ] ;
25 % Run data in t o system
26 [ normal ized ] = step ( normal i ze hd l , datain , darkin ,
l i g h t i n , meanin , s tddev in ) ;
27 ou tput h i s t o ry { I+1,J+1} = [ normal ized ] ;
28 end ;
29 end ;
30 end ;
31
32 % la t ency = 4 ( su b t r a c t i on ) + 1 ( comparison ) + 3 (mult ) + 1 (
comparison ) +
33 % 4 ( su b t r a c t i on ) + 3 (mult )
34 l a t ency = 16 ;
148
35 for I = 1 : rows+columns−l a t ency
36 inputs = inpu t h i s t o r y { I }
37 normal ized ( I ) = output h i s t o ry { I+la t ency }
38
39 ac tua l ( I ) = normal ize ( inputs (1 ) , inputs (2 ) , inputs (3 ) , inputs
(4 ) , inputs (5 ) )
40 end ;
41
42 % Output r e s u l t s to f i l e
43 T = tab l e ( normalized ’ , actua l ’ ) ;
44 wr i t e t ab l e (T, ’normalize.xlsx’ , ’Range’ , ’B2’ , ’
WriteVariableNames’ , f a l s e ) ;
1 %
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
2 % inne r p r oduc t t b .m
3 % Testbench f o r inner produc t component
4 %
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
5 % Sta r t i n i t i a l i z a t i o n
6 i f (˜ exist (’data’ , ’var’ ) )
7 load (’data.mat’ ) ;
8 end ;
9
10 clc ;
11 i t e r a t i o n s = 1 ;
12 rows = 200 ;
13 columns = 1 ;
14 product hd l = hdlcos im top ; % Set up s imu la t i on o b j e c t
15 M = 0;
16 for K = 1 : i t e r a t i o n s
17 for J = 0 : columns − 1
18 for I = 0 : 4 : ( rows ) − 1
19 norm1 = normal ize ( data ( I+1,J+1) , dark ( I+1,J+1) ,
l i g h t I ( I+1,J+1) , means (1 , I+1) , s tddevI (1 , I+1) ) ;
20 norm2 = normal ize ( data ( I+2,J+1) , dark ( I+2,J+1) ,
l i g h t I ( I+2,J+1) , means (1 , I+2) , s tddevI (1 , I+2) ) ;
21 norm3 = normal ize ( data ( I+3,J+1) , dark ( I+3,J+1) ,
l i g h t I ( I+3,J+1) , means (1 , I+3) , s tddevI (1 , I+3) ) ;
22 norm4 = normal ize ( data ( I+4,J+1) , dark ( I+4,J+1) ,
l i g h t I ( I+4,J+1) , means (1 , I+4) , s tddevI (1 , I+4) ) ;
23 c l a s s 1 = c l a s s (1 , I+1) ;
24 c l a s s 2 = c l a s s (1 , I+2) ;
25 c l a s s 3 = c l a s s (1 , I+3) ;
26 c l a s s 4 = c l a s s (1 , I+4) ;
149
27 i n pu t h i s t o r y {M+1,J+1} = [ norm1 , norm2 , norm3 , norm4 ,
c l a s s 1 , c l a s s 2 , c l a s s 3 , c l a s s 4 ] ;
28 % Run data in t o system
29 [ p a r t i a l 1 , pa r t i a l 2 , pa r t i a l 3 , pa r t i a l 4 , sum out ] = step (
product hdl , norm1 , norm2 , norm3 , norm4 , c l a s s 1 ,
c l a s s 2 , c l a s s 3 , c l a s s 4 ) ;
30 ou tput h i s t o ry {M+1,J+1} = [ pa r t i a l 1 , pa r t i a l 2 , pa r t i a l 3 ,
pa r t i a l 4 , sum out ] ;
31 M=M+1;
32 end ;
33 end ;
34 end ;
35
36 % la t ency = 5 ( inner product )
37 % la t ency = 21 ( channel sum)
38 prev ious1 = s i n g l e (0 ) ;
39 prev ious2 = s i n g l e (0 ) ;
40 prev ious3 = s i n g l e (0 ) ;
41 prev ious4 = s i n g l e (0 ) ;
42 l a t ency = 14 ; %26;
43 for J=0: columns−1
44 for I = 0 : ( rows/4−1)−l a t ency
45 K=4∗ I ;
46 inputs = inpu t h i s t o r y { I+1,J+1};
47 sim = output h i s t o ry { I+1+latency , J+1}
48
49 norms = [ normal ize ( data (K+1,J+1) , dark (K+1,J+1) , l i g h t I (K
+1,J+1) , means (1 ,K+1) , s tddevI (1 ,K+1) ) . . .
50 normal ize ( data (K+2,J+1) , dark (K+2,J+1) , l i g h t I (K
+2,J+1) , means (1 ,K+2) , s tddevI (1 ,K+2) ) . . .
51 normal ize ( data (K+3,J+1) , dark (K+3,J+1) , l i g h t I (K
+3,J+1) , means (1 ,K+3) , s tddevI (1 ,K+3) ) . . .
52 normal ize ( data (K+4,J+1) , dark (K+4,J+1) , l i g h t I (K
+4,J+1) , means (1 ,K+4) , s tddevI (1 ,K+4) ) ] ;
53 actual sum1 = inner product ( inputs (1 ) , inputs (5 ) ,
prev ious1 ) ;
54 actual sum2 = inner product ( inputs (2 ) , inputs (6 ) ,
prev ious2 ) ;
55 actual sum3 = inner product ( inputs (3 ) , inputs (7 ) ,
prev ious3 ) ;
56 actual sum4 = inner product ( inputs (4 ) , inputs (8 ) ,
prev ious4 ) ;
57 prev ious1 = actual sum1 ;
58 prev ious2 = actual sum2 ;
59 prev ious3 = actual sum3 ;
60 prev ious4 = actual sum4 ;
61
150
62 tota l sum = actual sum1 + actual sum2 + actual sum3 +
actual sum4
63 end ;
64 end ;
1 %
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− −−−−−−−−−−
2 % re g r e s s i o n t b .m
3 % Testbench f o r r e g r e s s i on system
4 %
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
5 % Sta r t i n i t i a l i z a t i o n
6 i f (˜ exist (’datafi’ , ’var’ ) )
7 load (’test_data.mat’ ) ;
8 end ;
9
10 clc ;
11 l i n e s = 85 ;
12 bands = 160 ;
13 samples = 110 ; %1024;
14 c l a s s e s = 5 ; %20;
15
16 r e g r e s s i o n hd l = hdlcos im top ; % Set up s imu la t i on o b j e c t
17
18 in = f i ( 0 , 0 , 98 , 0 ) ;
19
20 %wr i t e i n t e r c e p t s
21 for K = 1 : c l a s s e s
22 address = b i t conca t ( f i ( 1 , 0 , 14 , 0 ) , f i (K, 0 , 1 0 , 0 ) , f i ( 0 , 0 , 8 , 0 ) ) ;
23 data = f i ( 0 , 0 , 32 , 0 ) ;
24 data . hex = num2hex( c l a s s (1 ,K) ) ;
25 s tep ( r e g r e s s i o n hd l , in , in , in , in , in , f i ( 0 , 0 , 1 , 0 ) , f i
( 0 , 0 , 1 , 0 ) , f i ( 1 , 0 , 1 , 0 ) , address , data ) ;
26 end ;
27
28 s tep ( r e g r e s s i o n hd l , in , in , in , in , in , f i ( 0 , 0 , 1 , 0 ) , f i ( 0 , 0 , 1 , 0 )
, f i ( 0 , 0 , 1 , 0 ) , f i ( 0 , 0 , 32 , 0 ) , f i ( 0 , 0 , 32 , 0 ) ) ;
29
30 for K = 1 : c l a s s e s
31 for J = 1 : ( bands /5) ∗8
32 % Address genera t ion
33 address = b i t conca t ( f i ( 1 , 0 , 14 , 0 ) , f i (K, 0 , 1 0 , 0 ) , f i ( J , 0 , 8 , 0 )
) ;
34 data = f i ( 0 , 0 , 32 , 0 ) ;
35 data . hex = num2hex( c l a s s ( J+1,K) ) ;
151
36
37 % Write c l a s s e s
38 s tep ( r e g r e s s i o n hd l , in , in , in , in , in , f i ( 0 , 0 , 1 , 0 ) , f i
( 0 , 0 , 1 , 0 ) , f i ( 1 , 0 , 1 , 0 ) , address , data ) ;
39 end ;
40 end ;
41
42 %Empty c l o c k c y c l e s
43 s tep ( r e g r e s s i o n hd l , in , in , in , in , in , f i ( 0 , 0 , 1 , 0 ) , f i ( 0 , 0 , 1 , 0 )
, f i ( 0 , 0 , 1 , 0 ) , f i ( 0 , 0 , 32 , 0 ) , f i ( 0 , 0 , 32 , 0 ) ) ;
44 s tep ( r e g r e s s i o n hd l , in , in , in , in , in , f i ( 0 , 0 , 1 , 0 ) , f i ( 0 , 0 , 1 , 0 )
, f i ( 0 , 0 , 1 , 0 ) , f i ( 0 , 0 , 32 , 0 ) , f i ( 0 , 0 , 32 , 0 ) ) ;
45 s tep ( r e g r e s s i o n hd l , in , in , in , in , in , f i ( 0 , 0 , 1 , 0 ) , f i ( 0 , 0 , 1 , 0 )
, f i ( 0 , 0 , 1 , 0 ) , f i ( 0 , 0 , 32 , 0 ) , f i ( 0 , 0 , 32 , 0 ) ) ;
46
47 %WRITE MEANS
48 for J = 1 : ( bands /5) ∗8
49 % Address genera t ion
50 address = b i t conca t ( f i ( 1 , 0 , 22 , 0 ) , f i ( J−1 ,0 ,10 ,0) ) ;
51 data = f i ( 0 , 0 , 32 , 0 ) ;
52 data . hex = num2hex(means (1 , J ) ) ;
53
54 % Write means
55 s tep ( r e g r e s s i o n hd l , in , in , in , in , in , f i ( 0 , 0 , 1 , 0 ) , f i
( 0 , 0 , 1 , 0 ) , f i ( 1 , 0 , 1 , 0 ) , address , data ) ;
56 s tep ( r e g r e s s i o n hd l , in , in , in , in , in , f i ( 0 , 0 , 1 , 0 ) , f i
( 0 , 0 , 1 , 0 ) , f i ( 1 , 0 , 1 , 0 ) , address , data ) ;
57 s tep ( r e g r e s s i o n hd l , in , in , in , in , in , f i ( 0 , 0 , 1 , 0 ) , f i
( 0 , 0 , 1 , 0 ) , f i ( 1 , 0 , 1 , 0 ) , address , data ) ;
58 s tep ( r e g r e s s i o n hd l , in , in , in , in , in , f i ( 0 , 0 , 1 , 0 ) , f i
( 0 , 0 , 1 , 0 ) , f i ( 1 , 0 , 1 , 0 ) , address , data ) ;
59 end ;
60
61 % Empty c l o c k c y c l e s
62 s tep ( r e g r e s s i o n hd l , in , in , in , in , in , f i ( 0 , 0 , 1 , 0 ) , f i ( 0 , 0 , 1 , 0 )
, f i ( 0 , 0 , 1 , 0 ) , f i ( 0 , 0 , 32 , 0 ) , f i ( 0 , 0 , 32 , 0 ) ) ;
63 s tep ( r e g r e s s i o n hd l , in , in , in , in , in , f i ( 0 , 0 , 1 , 0 ) , f i ( 0 , 0 , 1 , 0 )
, f i ( 0 , 0 , 1 , 0 ) , f i ( 0 , 0 , 32 , 0 ) , f i ( 0 , 0 , 32 , 0 ) ) ;
64 s tep ( r e g r e s s i o n hd l , in , in , in , in , in , f i ( 0 , 0 , 1 , 0 ) , f i ( 0 , 0 , 1 , 0 )
, f i ( 0 , 0 , 1 , 0 ) , f i ( 0 , 0 , 32 , 0 ) , f i ( 0 , 0 , 32 , 0 ) ) ;
65
66 %WRITE STDDEVI
67 for J = 1 : ( bands /5) ∗8
68 % Address genera t ion
69 address = b i t conca t ( f i ( 4 , 0 , 22 , 0 ) , f i ( J−1 ,0 ,10 ,0) ) ;
70 data = f i ( 0 , 0 , 32 , 0 ) ;
71 data . hex = num2hex( s tddevI (1 , J ) ) ;
152
72
73 % Write s t dd e v I
74 s tep ( r e g r e s s i o n hd l , in , in , in , in , in , f i ( 0 , 0 , 1 , 0 ) , f i
( 0 , 0 , 1 , 0 ) , f i ( 1 , 0 , 1 , 0 ) , address , data ) ;
75 s tep ( r e g r e s s i o n hd l , in , in , in , in , in , f i ( 0 , 0 , 1 , 0 ) , f i
( 0 , 0 , 1 , 0 ) , f i ( 1 , 0 , 1 , 0 ) , address , data ) ;
76 s tep ( r e g r e s s i o n hd l , in , in , in , in , in , f i ( 0 , 0 , 1 , 0 ) , f i
( 0 , 0 , 1 , 0 ) , f i ( 1 , 0 , 1 , 0 ) , address , data ) ;
77 s tep ( r e g r e s s i o n hd l , in , in , in , in , in , f i ( 0 , 0 , 1 , 0 ) , f i
( 0 , 0 , 1 , 0 ) , f i ( 1 , 0 , 1 , 0 ) , address , data ) ;
78 end ;
79
80 % Empty c l o c k c y c l e s
81 s tep ( r e g r e s s i o n hd l , in , in , in , in , in , f i ( 0 , 0 , 1 , 0 ) , f i ( 0 , 0 , 1 , 0 )
, f i ( 0 , 0 , 1 , 0 ) , f i ( 0 , 0 , 32 , 0 ) , f i ( 0 , 0 , 32 , 0 ) ) ;
82 s tep ( r e g r e s s i o n hd l , in , in , in , in , in , f i ( 0 , 0 , 1 , 0 ) , f i ( 0 , 0 , 1 , 0 )
, f i ( 0 , 0 , 1 , 0 ) , f i ( 0 , 0 , 32 , 0 ) , f i ( 0 , 0 , 32 , 0 ) ) ;
83 s tep ( r e g r e s s i o n hd l , in , in , in , in , in , f i ( 0 , 0 , 1 , 0 ) , f i ( 0 , 0 , 1 , 0 )
, f i ( 0 , 0 , 1 , 0 ) , f i ( 0 , 0 , 32 , 0 ) , f i ( 0 , 0 , 32 , 0 ) ) ;
84 s tep ( r e g r e s s i o n hd l , in , in , in , in , in , f i ( 0 , 0 , 1 , 0 ) , f i ( 0 , 0 , 1 , 0 )
, f i ( 0 , 0 , 1 , 0 ) , f i ( 0 , 0 , 32 , 0 ) , f i ( 0 , 0 , 32 , 0 ) ) ;
85
86 %READ CLASSES
87 for K = 1 : c l a s s e s
88 for J = 1 : ( bands /5)∗8+1
89 % Address genera t ion
90 address = b i t conca t ( f i ( 1 , 0 , 14 , 0 ) , f i (K, 0 , 1 0 , 0 ) , f i ( J
−1 ,0 ,8 ,0) ) ;
91 % Read c l a s s e s
92 s tep ( r e g r e s s i o n hd l , in , in , in , in , in , f i ( 0 , 0 , 1 , 0 ) , f i
( 1 , 0 , 1 , 0 ) , f i ( 0 , 0 , 1 , 0 ) , address , f i ( 0 , 0 , 32 , 0 ) ) ;
93 s tep ( r e g r e s s i o n hd l , in , in , in , in , in , f i ( 0 , 0 , 1 , 0 ) , f i
( 1 , 0 , 1 , 0 ) , f i ( 0 , 0 , 1 , 0 ) , address , f i ( 0 , 0 , 32 , 0 ) ) ;
94 s tep ( r e g r e s s i o n hd l , in , in , in , in , in , f i ( 0 , 0 , 1 , 0 ) , f i
( 1 , 0 , 1 , 0 ) , f i ( 0 , 0 , 1 , 0 ) , address , f i ( 0 , 0 , 32 , 0 ) ) ;
95 [ ˜ , c l a s s r e a d (J ,K) ] = step ( r e g r e s s i o n hd l , in , in , in ,
in , in , f i ( 0 , 0 , 1 , 0 ) , f i ( 1 , 0 , 1 , 0 ) , f i ( 0 , 0 , 1 , 0 ) , address
, f i ( 0 , 0 , 32 , 0 ) ) ;
96 end ;
97 end ;
98 T = tab l e ( c l a s s r e ad , c l a s s ) ;
99 wr i t e t ab l e (T, ’classes.xlsx’ , ’Range’ , ’B1’ ) ;
100
101 %READ MEANS
102 for J = 1 : ( bands /5) ∗8
103 % Address genera t ion
104 address = b i t conca t ( f i ( 1 , 0 , 22 , 0 ) , f i ( J−1 ,0 ,10 ,0) ) ;
153
105 % Read means
106 s tep ( r e g r e s s i o n hd l , in , in , in , in , in , f i ( 0 , 0 , 1 , 0 ) , f i
( 1 , 0 , 1 , 0 ) , f i ( 0 , 0 , 1 , 0 ) , address , f i ( 0 , 0 , 32 , 0 ) ) ;
107 s tep ( r e g r e s s i o n hd l , in , in , in , in , in , f i ( 0 , 0 , 1 , 0 ) , f i
( 1 , 0 , 1 , 0 ) , f i ( 0 , 0 , 1 , 0 ) , address , f i ( 0 , 0 , 32 , 0 ) ) ;
108 s tep ( r e g r e s s i o n hd l , in , in , in , in , in , f i ( 0 , 0 , 1 , 0 ) , f i
( 1 , 0 , 1 , 0 ) , f i ( 0 , 0 , 1 , 0 ) , address , f i ( 0 , 0 , 32 , 0 ) ) ;
109 [ ˜ , mean read (J , 1 ) ] = step ( r e g r e s s i o n hd l , in , in , in , in , in
, f i ( 0 , 0 , 1 , 0 ) , f i ( 1 , 0 , 1 , 0 ) , f i ( 0 , 0 , 1 , 0 ) , address , f i
( 0 , 0 , 32 , 0 ) ) ;
110 end ;
111 T = tab l e (mean read , means ’ ) ;
112 wr i t e t ab l e (T, ’means.xlsx’ , ’Range’ , ’B1’ ) ;
113
114 %READ STDDEVI
115 for J = 1 : ( bands /5) ∗8
116 % Address genera t ion
117 address = b i t conca t ( f i ( 4 , 0 , 22 , 0 ) , f i ( J−1 ,0 ,10 ,0) ) ;
118 % Read s t dd e v I
119 s tep ( r e g r e s s i o n hd l , in , in , in , in , in , f i ( 0 , 0 , 1 , 0 ) , f i
( 1 , 0 , 1 , 0 ) , f i ( 0 , 0 , 1 , 0 ) , address , f i ( 0 , 0 , 32 , 0 ) ) ;
120 s tep ( r e g r e s s i o n hd l , in , in , in , in , in , f i ( 0 , 0 , 1 , 0 ) , f i
( 1 , 0 , 1 , 0 ) , f i ( 0 , 0 , 1 , 0 ) , address , f i ( 0 , 0 , 32 , 0 ) ) ;
121 s tep ( r e g r e s s i o n hd l , in , in , in , in , in , f i ( 0 , 0 , 1 , 0 ) , f i
( 1 , 0 , 1 , 0 ) , f i ( 0 , 0 , 1 , 0 ) , address , f i ( 0 , 0 , 32 , 0 ) ) ;
122 [ ˜ , s tddev read (J , 1 ) ] = step ( r e g r e s s i o n hd l , in , in , in , in ,
in , f i ( 0 , 0 , 1 , 0 ) , f i ( 1 , 0 , 1 , 0 ) , f i ( 0 , 0 , 1 , 0 ) , address , f i
( 0 , 0 , 32 , 0 ) ) ;
123 end ;
124 T = tab l e ( stddev read , stddevI ’ ) ;
125 wr i t e t ab l e (T, ’stddevs.xlsx’ , ’Range’ , ’B1’ ) ;
126 %Set Enable
127 s tep ( r e g r e s s i o n hd l , in , in , in , in , in , f i ( 0 , 0 , 1 , 0 ) , f i ( 0 , 0 , 1 , 0 )
, f i ( 1 , 0 , 1 , 0 ) , f i ( 0 , 0 , 32 , 0 ) , f i ( 1 , 0 , 32 , 0 ) ) ;
128 % Set In t e r rup t Enable
129 s tep ( r e g r e s s i o n hd l , in , in , in , in , in , f i ( 0 , 0 , 1 , 0 ) , f i ( 0 , 0 , 1 , 0 )
, f i ( 1 , 0 , 1 , 0 ) , f i ( 1 , 0 , 32 , 0 ) , f i ( 1 , 0 , 32 , 0 ) ) ;
130 % End i n i t i a l i z a t i o n
131
132 for K = 1 : l i n e s
133 sum = s i n g l e ( zeros ( samples , c l a s s e s ) ) ;
134
135 for J = 0 : samples − 1
136 for I = 0 : 5 : bands − 1
137 % Sta r t t e s t data genera t ion
138 in1 = b i t conca t ( f i ( I , 0 , 8 , 0 ) , f i ( J , 0 , 1 0 , 0 ) , d a t a f i (K, I
+1,J+1) , l i g h t I f i ( I+1,J+1) , d a r k f i ( I+1,J+1) ) ;
154
139 in2 = b i t conca t ( f i ( I +1 ,0 ,8 ,0) , f i ( J , 0 , 1 0 , 0 ) , d a t a f i (K, I
+2,J+1) , l i g h t I f i ( I+2,J+1) , d a r k f i ( I+2,J+1) ) ;
140 in3 = b i t conca t ( f i ( I +2 ,0 ,8 ,0) , f i ( J , 0 , 1 0 , 0 ) , d a t a f i (K, I
+3,J+1) , l i g h t I f i ( I+3,J+1) , d a r k f i ( I+3,J+1) ) ;
141 in4 = b i t conca t ( f i ( I +3 ,0 ,8 ,0) , f i ( J , 0 , 1 0 , 0 ) , d a t a f i (K, I
+4,J+1) , l i g h t I f i ( I+4,J+1) , d a r k f i ( I+4,J+1) ) ;
142 in5 = b i t conca t ( f i ( I +4 ,0 ,8 ,0) , f i ( J , 0 , 1 0 , 0 ) , d a t a f i (K, I
+5,J+1) , l i g h t I f i ( I+5,J+1) , d a r k f i ( I+5,J+1) ) ;
143 % End t e s t data genera t ion
144
145 % Run data in t o system
146 s tep ( r e g r e s s i o n hd l , in1 , in2 , in3 , in4 , in5 , f i
( 1 , 0 , 1 , 0 ) , f i ( 0 , 0 , 1 , 0 ) , f i ( 0 , 0 , 1 , 0 ) , f i ( 0 , 0 , 32 , 0 ) ,
f i ( 0 , 0 , 32 , 0 ) ) ;
147 end ;
148 end ;
149 % Wait f o r i n t e r r u p t
150 i r q = f i ( 0 , 0 , 1 , 0 ) ;
151 while ( i r q . data ˜= 1)
152 [ i rq , ˜ ] = step ( r e g r e s s i o n hd l , in , in , in , in , in , f i
( 0 , 0 , 1 , 0 ) , f i ( 0 , 0 , 1 , 0 ) , f i ( 0 , 0 , 1 , 0 ) , f i ( 0 , 0 , 32 , 0 ) , f i
( 0 , 0 , 32 , 0 ) ) ;
153 end ;
154
155 for M = 1: c l a s s e s
156 for J = 0 : samples − 1
157 % Address genera t ion
158 address = b i t conca t ( f i ( 1 , 0 , 13 , 0 ) , f i (M, 0 , 6 , 0 ) , f i ( J
, 0 , 1 3 , 0 ) ) ;
159
160 % Read r e s u l t s
161 s tep ( r e g r e s s i o n hd l , in , in , in , in , in , f i ( 0 , 0 , 1 , 0 ) ,
f i ( 1 , 0 , 1 , 0 ) , f i ( 0 , 0 , 1 , 0 ) , address , f i ( 0 , 0 , 32 , 0 ) ) ;
162 s tep ( r e g r e s s i o n hd l , in , in , in , in , in , f i ( 0 , 0 , 1 , 0 ) ,
f i ( 1 , 0 , 1 , 0 ) , f i ( 0 , 0 , 1 , 0 ) , address , f i ( 0 , 0 , 32 , 0 ) ) ;
163 [ ˜ , sum( J+1,M) ] = step ( r e g r e s s i o n hd l , in , in , in , in
, in , f i ( 0 , 0 , 1 , 0 ) , f i ( 1 , 0 , 1 , 0 ) , f i ( 0 , 0 , 1 , 0 ) ,
address , f i ( 0 , 0 , 32 , 0 ) ) ;
164 end ;
165 end ;
166 % Clear In t e r rup t
167 s tep ( r e g r e s s i o n hd l , in , in , in , in , in , f i ( 0 , 0 , 1 , 0 ) , f i
( 0 , 0 , 1 , 0 ) , f i ( 1 , 0 , 1 , 0 ) , f i ( 2 , 0 , 32 , 0 ) , f i ( 1 , 0 , 32 , 0 ) ) ;
168 % Write r e s u l t s and expec ted to f i l e
169 T = tab l e (sum) ;
170 wr i t e t ab l e (T, ’results_lc.xlsx’ , ’Sheet’ , K, ’Range’ , ’B1’ ) ;
155
171 [ model , exact ] = c a l c u l a t i o n t e s t ( d a t a f i (K, 1 : bands , 1 :
samples ) , dark , l i g h t I , means test , s t ddev I t e s t ,
c l a s s t e s t ( : , 1 : c l a s s e s ) ,K) ;
172
173 end ;
174
175 save t e s t d a t a
176 clear ;
177 load t e s t d a t a
1 %
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
2 % normal ize .m
3 % Compute the normal va lue as done in l o g i s t i c r e g r e s s i on
c a l c u l a t i o n
4 %
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
5 function normal ized = normal ize ( data , dark , l i g h t I , mean, s tddevI
)
6 d i f f = max( s i n g l e ( data − dark ) , s i n g l e (0 ) ) ;
7 co r r e c t ed = min( s i n g l e ( d i f f .∗ l i g h t I ) , s i n g l e (1 ) ) ;
8 normal ized = s i n g l e ( ( c o r r e c t ed − mean) .∗ s tddevI ) ;
1 %
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
2 % inner produc t .m
3 % Compute the inner product as done in l o g i s t i c r e g r e s s i on
c a l c u l a t i o n
4 %
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
5 function par t i a l sum = inner product ( normalized , c l a s s , p rev ious )
6 product = s i n g l e ( normal ized ∗ c l a s s ) ;
7 par t i a l sum = s i n g l e ( product + prev ious ) ;
1 %
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
2 % c a l c u l a t i o n t e s t .m
3 % Compute the p r o b a b i l i t y us ing l o g i s t i c r e g r e s s i on and wr i t e to
spreadshee t
4 %
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
156
5 function [ model , exact ] = c a l c u l a t i o n t e s t ( da ta f i , dark , l i g h t I ,
mean in , s tddev I in , c l a s s i n , shee t )
6 [ ˜ , c l a s s e s ] = s ize ( c l a s s i n ) ;
7 [ ˜ , rows , columns ] = s ize ( d a t a f i ) ;
8 part ia l sum mode l = s i n g l e ( zeros ( columns , c l a s s e s ) ) ;
9 pa r t i a l sum exac t = zeros ( columns , c l a s s e s ) ;
10 for M = 1: c l a s s e s
11 for J = 1 : columns
12 prev ious mode l = c l a s s i n (1 ,M) ; %in t e r c e p t
13 p r ev i ou s a c tua l = double ( c l a s s i n (1 ,M) ) ;
14 for I = 1 : rows
15 norm = normal ize ( s i n g l e ( d a t a f i (1 , I , J ) ) , dark ( I , J ) ,
l i g h t I ( I , J ) , mean in ( I ) , s t ddev I i n ( I ) ) ;
16 part ia l sum mode l (J ,M) = inner product (norm,
c l a s s i n ( I+1,M) , prev ious mode l ) ;
17 prev ious mode l = part ia l sum mode l (J ,M) ;
18
19 pa r t i a l sum exac t (J ,M) = (min(max( double ( d a t a f i
(1 , I , J ) ) − double ( dark ( I , J ) ) , 0) .∗ double (
l i g h t I ( I , J ) ) , 1) . . .
20 − double ( mean in ( I ) ) ) .∗ double ( s t ddev I i n ( I )
) .∗ double ( c l a s s i n ( I+1,M) ) +
pr ev i ou s a c tua l ;
21 p r ev i ou s a c tua l = pa r t i a l sum exac t (J ,M) ;
22 end ;
23 end ;
24 end ;
25 model = part ia l sum mode l ;
26 exact = par t i a l sum exac t ;
27 T = tab l e (model , exact ) ;
28 wr i t e t ab l e (T, ’results_lc.xlsx’ , ’Sheet’ , sheet , ’Range’ , ’P1
’ ) ;