OpenCores
URL https://opencores.org/ocsvn/dirac/dirac/trunk

Subversion Repositories dirac

[/] [dirac/] [trunk/] [src/] [encoder/] [ARITHMETICCODER.vhd] - Blame information for rev 12

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 10 petebleack
 -- ***** BEGIN LICENSE BLOCK *****
2 2 petebleack
-- 
3 10 petebleack
-- $Id: ARITHMETICCODER.vhd,v 1.4 2006-10-05 16:17:13 petebleackley Exp $ $Name: not supported by cvs2svn $
4
-- *
5
-- * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6
-- *
7
-- * The contents of this file are subject to the Mozilla Public License
8
-- * Version 1.1 (the "License"); you may not use this file except in compliance
9
-- * with the License. You may obtain a copy of the License at
10
-- * http://www.mozilla.org/MPL/
11
-- *
12
-- * Software distributed under the License is distributed on an "AS IS" basis,
13
-- * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
14
-- * the specific language governing rights and limitations under the License.
15
-- *
16
-- * The Original Code is BBC Research and Development code.
17
-- *
18
-- * The Initial Developer of the Original Code is the British Broadcasting
19
-- * Corporation.
20
-- * Portions created by the Initial Developer are Copyright (C) 2004.
21
-- * All Rights Reserved.
22
-- *
23
-- * Contributor(s): Peter Bleackley (Original author)
24
-- *
25
-- * Alternatively, the contents of this file may be used under the terms of
26
-- * the GNU General Public License Version 2 (the "GPL"), or the GNU Lesser
27
-- * Public License Version 2.1 (the "LGPL"), in which case the provisions of
28
-- * the GPL or the LGPL are applicable instead of those above. If you wish to
29
-- * allow use of your version of this file only under the terms of the either
30
-- * the GPL or LGPL and not to allow others to use your version of this file
31
-- * under the MPL, indicate your decision by deleting the provisions above
32
-- * and replace them with the notice and other provisions required by the GPL
33
-- * or LGPL. If you do not delete the provisions above, a recipient may use
34
-- * your version of this file under the terms of any one of the MPL, the GPL
35
-- * or the LGPL.
36 2 petebleack
-- * ***** END LICENSE BLOCK ***** */
37
 
38
library IEEE;
39
use IEEE.STD_LOGIC_1164.ALL;
40
use IEEE.STD_LOGIC_ARITH.ALL;
41
use IEEE.STD_LOGIC_UNSIGNED.ALL;
42
 
43
--  Uncomment the following lines to use the declarations that are
44
--  provided for instantiating Xilinx primitive components.
45
--library UNISIM;
46
--use UNISIM.VComponents.all;
47
 
48
entity ARITHMETICCODER is
49
    Port ( ENABLE : in std_logic;
50
           DATA_IN : in std_logic;
51 5 petebleack
                          CONTEXT_ENABLE : in std_logic;
52
                          CONTEXT_IN : in std_logic_vector (5 downto 0);
53 8 petebleack
                          HALVECOUNTS_IN : in std_logic;
54
                          FLUSH : in std_logic;
55 2 petebleack
                          RESET : in std_logic;
56
           CLOCK : in std_logic;
57
           SENDING : out std_logic;
58 8 petebleack
           DATA_OUT : out std_logic;
59
                          FLUSH_COMPLETE : out std_logic);
60 2 petebleack
end ARITHMETICCODER;
61
 
62
architecture RTL of ARITHMETICCODER is
63 5 petebleack
 
64 2 petebleack
        component LIMIT_REGISTER
65
        generic(CONST: std_logic);
66
        port(     LOAD : in std_logic_vector(15 downto 0);
67
           SET_VALUE : in std_logic;
68
           SHIFT_ALL : in std_logic;
69
           SHIFT_MOST : in std_logic;
70
                          RESET : in std_logic;
71
           CLOCK : in std_logic;
72
           OUTPUT : out std_logic_vector(15 downto 0));
73
        end component LIMIT_REGISTER;
74
        component FOLLOW_COUNTER
75
        port    ( INCREMENT : in std_logic;
76
           TEST : in std_logic;
77
                          RESET : in std_logic;
78
           CLOCK : in std_logic;
79
           OUTPUT : out std_logic);
80
        end component FOLLOW_COUNTER;
81
        component CONVERGENCE_CHECK
82
        port    ( HIGH_MSB : in std_logic;
83
           LOW_MSB : in std_logic;
84
           HIGH_SECONDBIT : in std_logic;
85
           LOW_SECONDBIT : in std_logic;
86
           CHECK : in std_logic;
87
           TRIGGER_OUTPUT : out std_logic;
88
           TRIGGER_FOLLOW : out std_logic);
89
        end component CONVERGENCE_CHECK;
90
        component ARITHMETIC_UNIT
91
        port    ( DIFFERENCE : in std_logic_vector(15 downto 0);
92 10 petebleack
           PROB : in std_logic_vector(7 downto 0);
93 2 petebleack
                          LOW : in std_logic_vector(15 downto 0);
94
           ENABLE : in std_logic;
95
                          RESET :       in std_logic;
96
           CLOCK : in std_logic;
97
           DIFFERENCE_OUT0 : out std_logic_vector(15 downto 0);
98
                          DIFFERENCE_OUT1 : out std_logic_vector(15 downto 0);
99
           RESULT_OUT0 : out std_logic_vector(15 downto 0);
100
                          RESULT_OUT1 : out std_logic_vector(15 downto 0);
101
           DATA_LOAD : out std_logic);
102
        end component ARITHMETIC_UNIT;
103
        component OUTPUT_UNIT
104
        port ( ENABLE : in std_logic;
105
           DATA : in std_logic;
106
           FOLLOW : in std_logic;
107
                          RESET : in std_logic;
108
           CLOCK : in std_logic;
109
           SENDING : out std_logic;
110
                          DATA_OUT : out std_logic;
111
           FOLLOW_COUNTER_TEST : out std_logic;
112
           SHIFT : out std_logic);
113
        end component OUTPUT_UNIT;
114 5 petebleack
        component INPUT_CONTROL
115
        generic( WIDTH : integer range 1 to 16);
116
        port( ENABLE : in std_logic;
117
           DATA_IN : in std_logic_vector(WIDTH - 1 downto 0);
118
           BUFFER_CONTROL : in std_logic;
119
           DEMAND : in std_logic;
120
           RESET : in std_logic;
121
           CLOCK : in std_logic;
122
           SENDING : out std_logic;
123
           DATA_OUT : out std_logic_vector(WIDTH - 1 downto 0));
124
        end component INPUT_CONTROL;
125
        component CONTEXT_MANAGER
126
        port (  CONTEXT_NUMBER : in std_logic_vector(5 downto 0);
127 8 petebleack
                                SET : in std_logic;
128
                                UPDATE : in std_logic;
129
                                DATA_IN : in std_logic;
130
                                HALVECOUNTS : in std_logic;
131 5 petebleack
           RESET : in std_logic;
132
           CLOCK : in std_logic;
133 10 petebleack
           PROB : out std_logic_vector(7 downto 0);
134 8 petebleack
                          READY : out std_logic);
135 5 petebleack
        end component CONTEXT_MANAGER;
136 2 petebleack
        signal HIGH_SET : std_logic;
137
        signal LOW_SET  : std_logic;
138 8 petebleack
        signal TRIGGER_SHIFT : std_logic;
139 2 petebleack
        signal SHIFT_ALL :      std_logic;
140
        signal DIFFERENCE_SHIFT_ALL :   std_logic;
141
        signal SHIFT_MOST :     std_logic;
142
        signal ZERO_INPUT :     std_logic;
143
        signal ARITHMETIC_UNIT_ENABLE : std_logic;
144
        signal ARITHMETIC_UNIT_DATA_LOAD :      std_logic;
145
        signal CONVERGENCE_TEST :       std_logic;
146
        signal TRIGGER_OUTPUT : std_logic;
147
        signal FOLLOW_COUNTER_TEST :    std_logic;
148
        signal FOLLOW:  std_logic;
149
        signal DATA_LOAD: std_logic;
150
        signal OUTPUT_ACTIVE :  std_logic;
151
        signal CHECK :  std_logic;
152
        signal DELAYED_CHECK : std_logic;
153
        signal DATA_AVAILABLE : std_logic;
154
        signal BUFFERED_DATA : std_logic;
155
        signal BUFFER_INPUT :   std_logic;
156 5 petebleack
        signal NEWCONTEXT : std_logic;
157 2 petebleack
        signal ARITHMETIC_UNIT_DIFFERENCE_OUT0 : std_logic_vector (15 downto 0);
158
        signal ARITHMETIC_UNIT_DIFFERENCE_OUT1 :        std_logic_vector(15 downto 0);
159
        signal DIFFERENCE_IN : std_logic_vector (15 downto 0);
160
        signal ARITHMETIC_UNIT_RESULT_OUT0 :    std_logic_vector (15 downto 0);
161
        signal ARITHMETIC_UNIT_RESULT_OUT1 : std_logic_vector (15 downto 0);
162
        signal DIFFERENCE_OUT : std_logic_vector (15 downto 0);
163
        signal HIGH_OUT : std_logic_vector (15 downto 0);
164
        signal LOW_OUT : std_logic_vector (15 downto 0);
165 10 petebleack
        signal PROB : std_logic_vector (7 downto 0);
166 5 petebleack
        signal CONTEXT_SELECT : std_logic_vector (5 downto 0);
167
        signal PROB_AVAILABLE : std_logic;
168
        signal BUFFERCONTEXT :  std_logic;
169
        signal DATA_IN2 : std_logic_vector(0 downto 0);
170
        signal BUFFERED_DATA2 : std_logic_vector(0 downto 0);
171 8 petebleack
        signal CONTEXT_BUFFER_DATA_IN : std_logic_vector(7 downto 0);
172
        signal CONTEXT_BUFFER_DATA_OUT : std_logic_vector(7 downto 0);
173
--      signal LAST_FIVE_TRIGGERS : std_logic_vector(4 downto 0);
174
        signal HALVECOUNTS : std_logic;
175
        signal BUFFERED_FLUSH : std_logic;
176
        signal FLUSH_ENCODER : std_logic;
177
        signal SWITCHED_DATA : std_logic;
178
        signal CONVERGED : std_logic;
179
        signal INCREMENT_FOLLOW : std_logic;
180
        signal ALLOWHALVING : std_logic;
181
        signal RELEASE_CONTEXT : std_logic;
182
--      signal FETCH_FLUSH : std_logic;
183
        signal DEMAND_CONTEXT : std_logic;
184
        signal LOCK : std_logic;
185
        signal DEMAND_DATA : std_logic;
186
        signal HOLDCONTEXT : std_logic;
187 2 petebleack
begin
188
-- input buffering
189
INBUFFER:       INPUT_CONTROL
190 5 petebleack
        generic map(WIDTH => 1)
191 2 petebleack
        port map(ENABLE => ENABLE,
192 5 petebleack
        DATA_IN => DATA_IN2,
193 2 petebleack
        BUFFER_CONTROL => BUFFER_INPUT,
194 8 petebleack
        DEMAND => DEMAND_DATA,
195 2 petebleack
        RESET => RESET,
196
        CLOCK => CLOCK,
197
        SENDING => DATA_AVAILABLE,
198 5 petebleack
        DATA_OUT => BUFFERED_DATA2);
199 2 petebleack
 
200 5 petebleack
        DATA_IN2(0) <= DATA_IN;
201
        BUFFERED_DATA <= BUFFERED_DATA2(0);
202 8 petebleack
        DEMAND_DATA <= ARITHMETIC_UNIT_DATA_LOAD and not LOCK;
203 5 petebleack
 
204
CONTEXT_BUFFER: INPUT_CONTROL
205 8 petebleack
        generic map(WIDTH => 8)
206 5 petebleack
        port map(ENABLE => CONTEXT_ENABLE,
207 8 petebleack
        DATA_IN => CONTEXT_BUFFER_DATA_IN,
208 5 petebleack
        BUFFER_CONTROL =>        BUFFERCONTEXT,
209 8 petebleack
        DEMAND => DEMAND_CONTEXT,
210 5 petebleack
        RESET => RESET,
211
        CLOCK => CLOCK,
212
        SENDING => NEWCONTEXT,
213 8 petebleack
        DATA_OUT => CONTEXT_BUFFER_DATA_OUT);
214
 
215
        CONTEXT_BUFFER_DATA_IN <= (CONTEXT_IN & HALVECOUNTS_IN & FLUSH);
216
 
217
        CONTEXT_SELECT <= CONTEXT_BUFFER_DATA_OUT(7 downto 2);
218
        HALVECOUNTS <= CONTEXT_BUFFER_DATA_OUT(1) and ALLOWHALVING;
219
        BUFFERED_FLUSH <= CONTEXT_BUFFER_DATA_OUT(0) and CONVERGENCE_TEST and (CONVERGED nor SHIFT_MOST) and not LOCK;
220
 
221
 
222
 
223
 
224 2 petebleack
-- Specify the registers
225
HIGH: LIMIT_REGISTER
226
        generic map(CONST => '1')
227
        port map(  LOAD => ARITHMETIC_UNIT_RESULT_OUT0,
228
           SET_VALUE => HIGH_SET,
229
           SHIFT_ALL => SHIFT_ALL,
230
           SHIFT_MOST => SHIFT_MOST,
231
                          RESET => RESET,
232
           CLOCK => CLOCK,
233
           OUTPUT => HIGH_OUT);
234
 
235
DIFFERENCE: LIMIT_REGISTER
236
        generic map(CONST => '1')
237
        port map(  LOAD => DIFFERENCE_IN,
238
           SET_VALUE => DATA_LOAD,
239
           SHIFT_ALL => DIFFERENCE_SHIFT_ALL,
240
           SHIFT_MOST => '0',
241
                          RESET => RESET,
242
           CLOCK => CLOCK,
243
           OUTPUT => DIFFERENCE_OUT);
244
 
245
LOW: LIMIT_REGISTER
246
        generic map(CONST => '0')
247
        port map(  LOAD => ARITHMETIC_UNIT_RESULT_OUT1,
248
           SET_VALUE => LOW_SET,
249
           SHIFT_ALL => SHIFT_ALL,
250
           SHIFT_MOST => SHIFT_MOST,
251
                          RESET => RESET,
252
           CLOCK => CLOCK,
253
           OUTPUT => LOW_OUT);
254
 
255
-- The arithmetic
256
 
257
ARITH: ARITHMETIC_UNIT
258
        port map(DIFFERENCE => DIFFERENCE_OUT,
259
           PROB => PROB,
260
                          LOW => LOW_OUT,
261
           ENABLE => ARITHMETIC_UNIT_ENABLE,
262
                          RESET => RESET,
263
           CLOCK => CLOCK,
264
           DIFFERENCE_OUT0 => ARITHMETIC_UNIT_DIFFERENCE_OUT0,
265
                          DIFFERENCE_OUT1 => ARITHMETIC_UNIT_DIFFERENCE_OUT1,
266
           RESULT_OUT0 => ARITHMETIC_UNIT_RESULT_OUT0,
267
                          RESULT_OUT1 => ARITHMETIC_UNIT_RESULT_OUT1,
268
           DATA_LOAD => ARITHMETIC_UNIT_DATA_LOAD);
269
 
270
--The convergence checks
271
 
272
CONVERGE: CONVERGENCE_CHECK
273
        port map(HIGH_MSB => HIGH_OUT(15),
274
           LOW_MSB => LOW_OUT(15),
275
           HIGH_SECONDBIT => HIGH_OUT(14),
276
           LOW_SECONDBIT => LOW_OUT(14),
277
           CHECK => CONVERGENCE_TEST,
278 8 petebleack
           TRIGGER_OUTPUT => CONVERGED,
279 2 petebleack
           TRIGGER_FOLLOW => SHIFT_MOST);
280
 
281 8 petebleack
                                TRIGGER_OUTPUT <= CONVERGED or FLUSH_ENCODER;
282 2 petebleack
--The Follow Counter
283
 
284
FC:     FOLLOW_COUNTER
285 8 petebleack
        port map( INCREMENT => INCREMENT_FOLLOW,
286 2 petebleack
           TEST => FOLLOW_COUNTER_TEST,
287
                          RESET => RESET,
288
           CLOCK => CLOCK,
289
           OUTPUT => FOLLOW);
290 8 petebleack
                INCREMENT_FOLLOW <= SHIFT_MOST or BUFFERED_FLUSH;
291 2 petebleack
--The output unit
292
 
293
OUTPUT: OUTPUT_UNIT
294
        port map(ENABLE => TRIGGER_OUTPUT,
295 8 petebleack
           DATA => SWITCHED_DATA,
296 2 petebleack
           FOLLOW => FOLLOW,
297
                          RESET => RESET,
298
           CLOCK => CLOCK,
299
           SENDING => OUTPUT_ACTIVE,
300
                          DATA_OUT => DATA_OUT,
301
           FOLLOW_COUNTER_TEST => FOLLOW_COUNTER_TEST,
302 8 petebleack
           SHIFT => TRIGGER_SHIFT);
303 2 petebleack
 
304
        SENDING <= OUTPUT_ACTIVE;
305
 
306 8 petebleack
        SHIFT_ALL <= TRIGGER_SHIFT and not FLUSH_ENCODER;
307
 
308 2 petebleack
-- Input logic
309
 
310
        DATA_LOAD <= DATA_AVAILABLE and ARITHMETIC_UNIT_DATA_LOAD;
311
        HIGH_SET <= ZERO_INPUT and DATA_LOAD;
312
        ZERO_INPUT <= not BUFFERED_DATA;
313
        LOW_SET <= BUFFERED_DATA and DATA_LOAD;
314
 
315 8 petebleack
FLUSH_SWITCH : process (FLUSH_ENCODER,LOW_OUT,HIGH_OUT)
316
begin
317
        if FLUSH_ENCODER = '1' then
318
                SWITCHED_DATA <= LOW_OUT(14);
319
        else
320
                SWITCHED_DATA <= HIGH_OUT(15);
321
        end if;
322
end process FLUSH_SWITCH;
323
 
324 2 petebleack
-- Control logic for DIFFERENCE register
325
 
326
        DIFFERENCE_SHIFT_ALL <= SHIFT_ALL or SHIFT_MOST;
327
 
328
-- Control logic for convergence check
329
 
330
        CHECK <= DIFFERENCE_SHIFT_ALL or DATA_LOAD;
331
 
332 5 petebleack
CONVERGENCE_TEST_DELAY: process (CLOCK)
333
        begin
334
        if CLOCK'event and CLOCK = '1' then
335
                DELAYED_CHECK <= CHECK;
336
        end if;
337
        end process CONVERGENCE_TEST_DELAY;
338 2 petebleack
 
339
        CONVERGENCE_TEST <= DELAYED_CHECK or FOLLOW_COUNTER_TEST;
340
 
341
-- Control logic for arithmetic unit
342
 
343 5 petebleack
        ARITHMETIC_UNIT_ENABLE <= PROB_AVAILABLE and not(OUTPUT_ACTIVE or DIFFERENCE_SHIFT_ALL or DATA_LOAD);
344 2 petebleack
 
345
-- Control Logic for input control
346
 
347
        BUFFER_INPUT <= OUTPUT_ACTIVE or not ARITHMETIC_UNIT_DATA_LOAD;
348
 
349
-- Select the new difference value
350
 
351
NEWDIFF : process(BUFFERED_DATA,ARITHMETIC_UNIT_DIFFERENCE_OUT0,ARITHMETIC_UNIT_DIFFERENCE_OUT1)
352
        begin
353
                if(BUFFERED_DATA = '1') then
354
                        DIFFERENCE_IN <= ARITHMETIC_UNIT_DIFFERENCE_OUT1;
355
                else
356
                        DIFFERENCE_IN <= ARITHMETIC_UNIT_DIFFERENCE_OUT0;
357
                end if;
358
        end process NEWDIFF;
359
 
360 5 petebleack
-- Select the context
361
 
362
PROBABILITY : CONTEXT_MANAGER
363
                        port map(CONTEXT_NUMBER => CONTEXT_SELECT,
364 8 petebleack
                        SET => NEWCONTEXT,
365
                        UPDATE => DATA_LOAD,
366
                        DATA_IN => BUFFERED_DATA,
367
                        HALVECOUNTS => HALVECOUNTS,
368 5 petebleack
                        RESET => RESET,
369
                        CLOCK => CLOCK,
370 8 petebleack
                        PROB => PROB,
371
                        READY => PROB_AVAILABLE);
372 5 petebleack
 
373 8 petebleack
 
374
 
375
FLUSH_CONTROL : process (CLOCK)
376
begin
377
        if CLOCK'event and CLOCK = '1' then
378
                if RESET = '1' then
379
                        FLUSH_ENCODER <= '0';
380
                elsif BUFFERED_FLUSH = '1'  then
381
                        FLUSH_ENCODER <= '1';
382
                elsif TRIGGER_SHIFT = '1' then
383
                        FLUSH_ENCODER <= '0';
384
                end if;
385 5 petebleack
        end if;
386 8 petebleack
end process     FLUSH_CONTROL;
387
 
388
FLUSH_COMPLETE <= FLUSH_ENCODER and TRIGGER_SHIFT;
389
 
390
LIMITHALVING : process (CLOCK)
391
begin
392
        if CLOCK'event and CLOCK='1' then
393
                if RESET='1' then
394
                        ALLOWHALVING <= '1';
395
                else
396
                        ALLOWHALVING <= not CONTEXT_BUFFER_DATA_OUT(1);
397
                end if;
398 5 petebleack
        end if;
399 8 petebleack
end process LIMITHALVING;
400 5 petebleack
 
401 8 petebleack
RELEASE_CONTEXT <= RESET or DEMAND_CONTEXT;
402 5 petebleack
 
403 8 petebleack
CONTEXTS_SHOULD_BE_BUFFERED : process (RELEASE_CONTEXT,CLOCK)
404
begin
405
        if CLOCK'event and CLOCK = '1' then
406
                if NEWCONTEXT = '1' then
407
                        HOLDCONTEXT <= '1';
408
                elsif RELEASE_CONTEXT = '1' then
409
                        HOLDCONTEXT <= '0';
410
                end if;
411
        end if;
412
end process CONTEXTS_SHOULD_BE_BUFFERED;
413
 
414
BUFFERCONTEXT <= HOLDCONTEXT and not RELEASE_CONTEXT;
415
 
416
--STORE_TRIGGERS : process (CLOCK)
417
--begin
418
--      if CLOCK'event and CLOCK='1' then
419
--              if RESET='1' then
420
--                      LAST_FIVE_TRIGGERS <= "11111";
421
--              else
422
--                      LAST_FIVE_TRIGGERS <= LAST_FIVE_TRIGGERS(3 downto 0) & (DATA_AVAILABLE or OUTPUT_ACTIVE or DIFFERENCE_SHIFT_ALL);
423
--              end if;
424
--      end if;
425
--end process STORE_TRIGGERS;
426
 
427
--GET_FLUSH_SIGNAL : process (LAST_FIVE_TRIGGERS)
428
--begin
429
--      if LAST_FIVE_TRIGGERS = "00000" then
430
--              FETCH_FLUSH <= '1';
431
--      else
432
--              FETCH_FLUSH <= '0';
433
--      end if;
434
--end process GET_FLUSH_SIGNAL;
435
 
436
LOCK_ENCODER : process (CLOCK)
437
begin
438
        if CLOCK'event and CLOCK='1' then
439
                if RESET = '1' then
440
                        LOCK <= '0';
441
                elsif BUFFERED_FLUSH='1' then
442
                        LOCK <= '1';
443
                end if;
444
        end if;
445
end process LOCK_ENCODER;
446
 
447
DEMAND_CONTEXT <= DATA_LOAD; -- or FETCH_FLUSH;--DATA_LOAD or FETCH_FLUSH or AFTER_RESET;
448
 
449
--DELAY_RESET: process (CLOCK)
450
--begin
451
--      if CLOCK'event and CLOCK='1' then
452
--              AFTER_RESET <= RESET;
453
--      end if;
454
--end process DELAY_RESET;
455 5 petebleack
--
456
 
457 2 petebleack
end RTL;

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.