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

Subversion Repositories ima_adpcm_encoder

[/] [ima_adpcm_encoder/] [trunk/] [IMA_ADPCM_top.vhd] - Blame information for rev 8

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 6 galland
----------------------------------------------------------------------------------
2
-- Company:       VISENGI S.L. (www.visengi.com)
3
-- Engineer:      Victor Lopez Lorenzo (victor.lopez (at) visengi (dot) com)
4
-- 
5 7 galland
-- Create Date:    19:34:36 04/November/2008
6 6 galland
-- Project Name:   IMA ADPCM Encoder
7
-- Tool versions:  Xilinx ISE 9.2i
8
-- Description: 
9
--
10
-- Description: This project features a full-hardware sound compressor using the well known algorithm IMA ADPCM.
11
--              The core acts as a slave WISHBONE device. The output is perfectly compatible with any sound player
12
--              with the IMA ADPCM codec (included by default in every Windows). Includes a testbench that takes
13
--              an uncompressed PCM 16 bits Mono WAV file and outputs an IMA ADPCM compressed WAV file.
14
--              Compression ratio is fixed for IMA-ADPCM, being 4:1.
15
--
16
--
17 7 galland
-- LICENSE TERMS: GNU GENERAL PUBLIC LICENSE Version 3
18
--
19
--     That is you may use it only in NON-COMMERCIAL projects.
20
--     You are only required to include in the copyrights/about section 
21
--     that your system contains a "IMA ADPCM Encoder (C) VISENGI S.L. under GPL license"
22 6 galland
--     This holds also in the case where you modify the core, as the resulting core
23
--     would be a derived work.
24
--     Also, we would like to know if you use this core in a project of yours, just an email will do.
25
--
26 7 galland
--    Please take good note of the disclaimer section of the GPL license, as we don't
27 6 galland
--    take any responsability for anything that this core does.
28
----------------------------------------------------------------------------------
29
 
30
library IEEE;
31
use IEEE.STD_LOGIC_1164.ALL;
32
use IEEE.STD_LOGIC_ARITH.ALL;
33
use IEEE.STD_LOGIC_UNSIGNED.ALL;
34
 
35
entity IMA_ADPCM_top is
36
   Port (  wb_clk_i : in std_logic;
37
           wb_rst_i : in std_logic;
38
           wb_cyc_i : in std_logic;
39
           wb_stb_i : in std_logic;
40
           wb_we_i  : in std_logic;
41
           wb_adr_i : in std_logic_vector(1 downto 0);
42
           wb_dat_i : in std_logic_vector(15 downto 0);
43
           wb_dat_o : out std_logic_vector(15 downto 0);
44
           wb_ack_o : out std_logic);
45
end IMA_ADPCM_top;
46
 
47
architecture Behavioral of IMA_ADPCM_top is
48
   component IMA_ADPCM_Encode port (
49
           clk : in  STD_LOGIC;
50
           reset : in  STD_LOGIC;
51
           PredictedValue_o : out std_logic_vector(15 downto 0);
52
           StepIndex_o : out std_logic_vector(6 downto 0);
53
           StateRDY : out std_logic;
54
                          sample : in std_logic_vector(15 downto 0); --don't change it while sample_rdy='1'
55
           sample_rdy : in std_logic; --lower it only when ADPCM_sample_rdy = '1'
56
                          ADPCM_sample : out std_logic_vector(3 downto 0);
57
           ADPCM_sample_rdy : out std_logic);
58
   end component;
59
 
60
        component WAV_header_rom port(
61
                addr0   : in  STD_LOGIC_VECTOR(5 downto 0);
62
                clk   : in  STD_LOGIC;
63
                datao0: out STD_LOGIC_VECTOR(7 downto 0));
64
        end component;
65
 
66
   signal WAV_addr : std_logic_vector(5 downto 0);
67
   signal WAV_data : std_logic_vector(7 downto 0);
68
 
69
   signal sample : std_logic_vector(15 downto 0);
70
   signal ADPCM_sample : std_logic_vector(3 downto 0);
71
   signal sample_rdy, ADPCM_sample_rdy : std_logic;
72
 
73
   signal soft_reset, isoft_reset : std_logic;
74
   signal iwb_ack_o : std_logic;
75
 
76
 
77
   -- IMPORTANT: Remember that flow control MUST be done externally (that is, if SamplesPerSec is set to 8000, then 8000 samples must be fed every second)
78
   -- The first 60 bytes of data correspond to the WAV header
79
   --
80
   --WISHBONE REGS' DESCRIPTION:
81
   --reg 0: Control(W: WRITES)/Status(R: READS)
82
   --    bit 15: (R) 0=finished, 1=compressing (W) 0=end file (if compressing), 1=start file compression
83
   --          clearing this bit while compressing doesn't immediately end it, because the current block must be finished (block size=256 bytes)
84
   --                the only thing that happens is that the next samples up to the end of the block (samples_per_block=505) will be zeros (silence).
85
   --    bit  1: (R) 1=ready for new input sample to be written on reg 3 (auto-cleared on write to reg 3), 0=processing last input sample
86
   --    bit  0: (R) 1=16 bits of output file are ready to be read on reg 3 (auto-cleared on read to reg 3), 0=processing next output word
87
   --reg 1: SamplesPerSec (i.e.: 8000, 22050, 32000, 44100, ...)
88
   --reg 2: SecondsToCompress (0 for undefined (max values will be used in file headers) -> the sound will be complete and reproducible but an "early EOF" or "damaged file" error may appear in player at the end of the sound)
89
   --reg 3: On writes: a 16 bit sample is expected - On reads: data lines will contain 16 bits of the compressed output file (only if reg(0)(0)='1'!!!)
90
      --output words of 16 bits should be read from MSB to LSB
91
 
92
   signal SamplesPerSec1, SecondsToCompress2, CompressedWord : std_logic_vector(15 downto 0);
93
   signal InputRDY, OutputRDY, OutputRDY_sync2, RDYOutput, FinishBlock : std_logic;
94
   signal StartCompressing, Compressing, EndCompression : std_logic;
95
   signal CompressedNibbles : integer range 0 to 4;
96
 
97
   signal WriteHeader, WriteHeader_sync2 : std_logic;
98
   signal WriteState, WriteState_sync2 : std_logic;
99
   signal BlockBytes : integer range 0 to 255;
100
   signal SecondsCompressed : std_logic_vector(15 downto 0);
101
   signal SamplesCompressed : std_logic_vector(15 downto 0);
102
   signal MSB, LoadROM : std_logic;
103
   signal SamplesInFile : std_logic_vector(31 downto 0);
104
 
105
        signal PredictedValue : std_logic_vector(15 downto 0);
106
        signal StepIndex : std_logic_vector(6 downto 0);
107
   signal StateRDY : std_logic;
108
   signal WriteStateLSWord : std_logic;
109
begin
110
   isoft_reset <= soft_reset or wb_rst_i;
111
 
112
   WAV_ROM: WAV_header_rom port map (
113
      addr0 => WAV_addr,
114
      clk => wb_clk_i,
115
      datao0 => WAV_data);
116
 
117
   IMA_ADPCM_Encoder : IMA_ADPCM_Encode port map (
118
      clk => wb_clk_i,
119
      reset => isoft_reset,
120
      PredictedValue_o => PredictedValue,
121
      StepIndex_o => StepIndex,
122
      StateRDY => StateRDY,
123
      sample => sample,
124
      sample_rdy => sample_rdy,
125
      ADPCM_sample => ADPCM_sample,
126
      ADPCM_sample_rdy => ADPCM_sample_rdy);
127
 
128
   Input_process : process (wb_clk_i, wb_rst_i)
129
      variable WaitW : std_logic;
130
   begin
131
      if (wb_rst_i = '1') then
132
         iwb_ack_o <= '0';
133
         wb_dat_o <= (others => '0');
134
         InputRDY <= '0';
135
         OutputRDY <= '0';
136
         OutputRDY_sync2 <= '0';
137
         SamplesPerSec1 <= (others => '0');
138
         SecondsToCompress2 <= (others => '0');
139
         Compressing <= '0';
140
         StartCompressing <= '0';
141
         WaitW := '0';
142
         soft_reset <= '0';
143
 
144
         sample <= (others => '0');
145
         sample_rdy <= '0';
146
      elsif (wb_clk_i = '1' and wb_clk_i'event) then
147
         OutputRDY_sync2 <= OutputRDY;
148
         soft_reset <= '0';
149
         iwb_ack_o <= wb_cyc_i and wb_stb_i and not iwb_ack_o;
150
 
151
         if (RDYOutput = '1') then OutputRDY <= '1'; end if;
152
         if (ADPCM_sample_rdy = '1') then
153
            sample_rdy <= '0';
154
            if (CompressedNibbles < 3 and WriteState = '0') then InputRDY <= '1'; end if;
155
         end if;
156
 
157
         if (OutputRDY_sync2 = '1' and OutputRDY = '0' and WriteState = '0' and WriteHeader = '0') then InputRDY <= '1'; end if;
158
         if (((OutputRDY_sync2 = '1' and OutputRDY = '0') or (WriteHeader_sync2 = '1' and WriteHeader = '0')) and WriteState = '1' and BlockBytes = 0 and WaitW = '0') then InputRDY <= '1'; WaitW := '1'; end if; --to write the block header one input sample is required
159
         if (WriteState_sync2 = '1' and WriteState = '0') then InputRDY <= '1'; WaitW := '0'; end if; --after writing the block header, ask for input sample
160
 
161
 
162
         if (EndCompression = '1') then Compressing <= '0'; end if;
163
 
164
         if (FinishBlock = '1') then
165
            sample <= x"0000";
166
            sample_rdy <= '1';
167
            InputRDY <= '0';
168
         end if;
169
 
170
         if (wb_cyc_i = '1' and wb_stb_i = '1') then
171
            case wb_adr_i is
172
               when "00" => --control/status
173
                  if (wb_we_i = '0') then --read = status
174
                     wb_dat_o <= (others => '0');
175
                     wb_dat_o(15) <= Compressing;
176
                     wb_dat_o(1) <= InputRDY;
177
                     wb_dat_o(0) <= OutputRDY;
178
                  else --write = control
179
                     StartCompressing <= wb_dat_i(15);
180
                     if (wb_dat_i(15) = '1' and Compressing = '0') then --start new operation?
181
                        soft_reset <= '1';
182
                        OutputRDY <= '0';
183
                        InputRDY <= '0'; --wait for WriteHeader
184
                        Compressing <= '1';
185
                     end if;
186
                  end if;
187
               when "11" => --data in/out
188
                  if (wb_we_i = '0') then --read = get compressed data
189
                     if (OutputRDY = '1') then
190
                        wb_dat_o <= CompressedWord;
191
                        OutputRDY <= '0';
192
                     end if;
193
                  else --write = put input sample
194
                     if (InputRDY = '1') then
195
                        if (FinishBlock = '0') then sample <= wb_dat_i; else sample <= x"0000"; end if;
196
                        sample_rdy <= '1';
197
                        InputRDY <= '0';
198
                     end if;
199
                  end if;
200
               when "01" => --SamplesPerSec
201
                  if (wb_we_i = '0') then --read
202
                     wb_dat_o <= SamplesPerSec1;
203
                  else --write
204
                     SamplesPerSec1 <= wb_dat_i;
205
                  end if;
206
               when "10" => --SecondsToCompress
207
                  if (wb_we_i = '0') then --read
208
                     wb_dat_o <= SecondsToCompress2;
209
                  else --write
210
                     SecondsToCompress2 <= wb_dat_i;
211
                  end if;
212
               when others =>
213
                  report "-----------> Wrong WB Address!" severity WARNING;
214
            end case;
215
         end if;
216
      end if;
217
   end process;
218
 
219
   wb_ack_o <= iwb_ack_o;
220
 
221
 
222
   Output_process : process (wb_clk_i, wb_rst_i)
223
      variable WaitADPCM : std_logic;
224
      variable WAV_data_to_write : std_logic_vector(7 downto 0);
225
   begin
226
      if (wb_rst_i = '1') then
227
         CompressedWord <= (others => '0');
228
         RDYOutput <= '0';
229
         WriteHeader <= '1';
230
         WriteHeader_sync2 <= '1';
231
         FinishBlock <= '0';
232
         BlockBytes <= 0;
233
         CompressedNibbles <= 0;
234
         EndCompression <= '0';
235
         MSB <= '1';
236
         LoadROM <= '1';
237
         WAV_addr <= (others => '0');
238
         WAV_data_to_write := (others => '0');
239
 
240
         SecondsCompressed <= (others => '0');
241
         SamplesCompressed <= (others => '0');
242
 
243
         WriteState <= '0';
244
         WriteState_sync2 <= '0';
245
         WriteStateLSWord  <= '0';
246
         WaitADPCM := '0';
247
      elsif (wb_clk_i = '1' and wb_clk_i'event) then
248
         WriteHeader_sync2 <= WriteHeader;
249
         WriteState_sync2 <= WriteState;
250
 
251
         if (WriteState_sync2 = '0' and WriteState = '1') then WaitADPCM := '1'; end if;
252
 
253
         EndCompression <= '0';
254
         RDYOutput <= '0';
255
         if (StartCompressing = '0' and Compressing = '1') then FinishBlock <= '1'; end if; --finish compression on end of current block!
256
 
257
         if (ADPCM_sample_rdy = '1' and WriteState = '0') then --because WriteState uses one ADPCM_sample_rdy to write its own header sample
258
            case CompressedNibbles is --little endian on each byte (byte 0:n1n0, byte 1:n3n2, ...)
259
               when 0 =>
260
                  CompressedWord(11 downto 8) <= ADPCM_sample;
261
                  CompressedNibbles <= CompressedNibbles + 1;
262
               when 1 =>
263
                  CompressedWord(15 downto 12) <= ADPCM_sample;
264
                  CompressedNibbles <= CompressedNibbles + 1;
265
               when 2 =>
266
                  CompressedWord(3 downto 0) <= ADPCM_sample;
267
                  CompressedNibbles <= CompressedNibbles + 1;
268
               when 3 =>
269
                  CompressedWord(7 downto 4) <= ADPCM_sample;
270
                  CompressedNibbles <= CompressedNibbles + 1;
271
                  if (BlockBytes /= 254) then
272
                     BlockBytes <= BlockBytes + 2;
273
                  else
274
                     BlockBytes <= 0;
275
                     WriteState <= '1';
276
                  end if;
277
               when others => --4 pending wb read of reg 3
278
                  CompressedNibbles <= CompressedNibbles;
279
            end case;
280
 
281
 
282
            if (SamplesCompressed = SamplesPerSec1) then
283
               if (SecondsCompressed = SecondsToCompress2) then
284
                  FinishBlock <= '1'; --finish compression on end of current block!
285
               else
286
                  SamplesCompressed <= (others => '0');
287
                  SecondsCompressed <= SecondsCompressed + 1;
288
               end if;
289
            else
290
               SamplesCompressed <= SamplesCompressed + 1;
291
            end if;
292
         end if;
293
 
294
         if (Compressing = '1' and EndCompression = '0') then
295
            if (CompressedNibbles = 4) then
296
               if (OutputRDY_sync2 = '1' and OutputRDY = '0') then
297
                  CompressedNibbles <= 0;
298
                  if (WriteStateLSWord  = '0' and BlockBytes = 4) then WriteState <= '0'; end if; --lower it (but not in the cycle it is risen!)
299
               else
300
                  if (OutputRDY = '0' and RDYOutput = '0') then RDYOutput <= '1'; end if;
301
               end if;
302
            else
303
               if (RDYOutput = '0' and OutputRDY = '0') then --get next nibble/s if last CompressedWord is read
304
                  if (FinishBlock = '1') then --finish compression on end of current block!
305
                     if (BlockBytes = 0) then
306
                        EndCompression <= '1'; --signal input process the end of the compression
307
                     end if;
308
                  else --normal operation
309
                     if (WriteHeader = '1') then --output WAV header
310
                        if (LoadROM = '0') then
311
                           LoadROM <= '1'; --give the ROM time to read a byte
312
                           if (WAV_addr = "111100") then --last header byte + 1
313
                              WAV_addr <= (others => '0');
314
                              WriteHeader <= '0';
315
                              WriteState <= '1';
316
                           end if;
317
                        else
318
                           WAV_addr <= WAV_addr + 1;
319
                           WAV_data_to_write := WAV_data; --by default times are for undefined in WAV header
320
                           case WAV_addr is --all data is little-endian!
321
                              when "000100" => if (SecondsToCompress2 /= x"0000") then WAV_data_to_write := SamplesInFile(7 downto 0); end if; --LSB of file_size-8 (FF for undefined)
322
                              when "000101" => if (SecondsToCompress2 /= x"0000") then WAV_data_to_write := SamplesInFile(15 downto 8); end if; --LSB2 of file_size-8 (FF for undefined)
323
                              when "000110" => if (SecondsToCompress2 /= x"0000") then WAV_data_to_write := SamplesInFile(23 downto 16); end if; --MSB2 of file_size-8 (FF for undefined)
324
                              when "000111" => if (SecondsToCompress2 /= x"0000") then WAV_data_to_write := SamplesInFile(31 downto 24); end if; --MSB of file_size-8 (7F for undefined)
325
 
326
                              when "011000" => WAV_data_to_write := SamplesPerSec1(7 downto 0); --LSB of SamplesPerSec
327
                              when "011001" => WAV_data_to_write := SamplesPerSec1(15 downto 8); --MSB of SamplesPerSec
328
                              when "011100" => WAV_data_to_write := SamplesPerSec1(8 downto 1); --LSB of AvgBytesPerSec : approx. SamplesPerSec/2
329
                              when "011101" => WAV_data_to_write := '0' & SamplesPerSec1(15 downto 9); --MSB of AvgBytesPerSec : approx. SamplesPerSec/2
330
 
331
                              when "110000" => if (SecondsToCompress2 /= x"0000") then WAV_data_to_write := SamplesInFile(7 downto 0); end if; --LSB of SamplesPerChannelInFile (FF for undefined)
332
                              when "110001" => if (SecondsToCompress2 /= x"0000") then WAV_data_to_write := SamplesInFile(15 downto 8); end if; --LSB2 of SamplesPerChannelInFile (FF for undefined)
333
                              when "110010" => if (SecondsToCompress2 /= x"0000") then WAV_data_to_write := SamplesInFile(23 downto 16); end if; --MSB2 of SamplesPerChannelInFile (FF for undefined)
334
                              when "110011" => if (SecondsToCompress2 /= x"0000") then WAV_data_to_write := SamplesInFile(31 downto 24); end if; --MSB of SamplesPerChannelInFile (7F for undefined)
335
 
336
                              when "111000" => if (SecondsToCompress2 /= x"0000") then WAV_data_to_write := SamplesInFile(7 downto 0); end if; --LSB of file_size-60 (FF for undefined)
337
                              when "111001" => if (SecondsToCompress2 /= x"0000") then WAV_data_to_write := SamplesInFile(15 downto 8); end if; --LSB2 of file_size-60 (FF for undefined)
338
                              when "111010" => if (SecondsToCompress2 /= x"0000") then WAV_data_to_write := SamplesInFile(23 downto 16); end if; --MSB2 of file_size-60 (FF for undefined)
339
                              when "111011" => if (SecondsToCompress2 /= x"0000") then WAV_data_to_write := SamplesInFile(31 downto 24); end if; --MSB of file_size-60 (7F for undefined)
340
                              when others => null;
341
                           end case;
342
                           if (MSB = '1') then
343
                              CompressedWord(15 downto 8) <= WAV_data_to_write;
344
                           else
345
                              CompressedWord(7 downto 0) <= WAV_data_to_write;
346
                              CompressedNibbles <= 4; --signal word ready
347
                           end if;
348
                           MSB <= not MSB;
349
                           LoadROM <= '0';
350
                        end if;
351
                     else --output ADPCM data
352
                        if (WriteState = '1' and FinishBlock = '0') then --block header with state at the beginning and every 256 bytes
353
                           if (WaitADPCM = '1') then
354
                              if (ADPCM_sample_rdy = '1') then WaitADPCM := '0'; end if;
355
                           else
356
                              if (StateRDY = '1') then
357
                                 if (WriteStateLSWord  = '0') then --write first two bytes of state: PredictedValue
358
                                    CompressedWord <= PredictedValue(7 downto 0) & PredictedValue(15 downto 8); --little endian!
359
                                    BlockBytes <= 2;
360
                                    SamplesCompressed <= SamplesCompressed + 1;
361
                                 else --write second two bytes of state: StepIndex & x"00"
362
                                    CompressedWord <= '0' & StepIndex & x"00";
363
                                    --WriteState <= '0'; lower it only after Output written
364
                                    BlockBytes <= BlockBytes + 2;
365
                                 end if;
366
                                 WriteStateLSWord <= not WriteStateLSWord;
367
                                 CompressedNibbles <= 4; --signal word ready
368
                              end if;
369
                           end if;
370
                        else --data nibbles
371
                           null; --taken care of above
372
                        end if;
373
                     end if;
374
                  end if;
375
               end if;
376
            end if;
377
         else
378
            CompressedWord <= (others => '0');
379
            RDYOutput <= '0';
380
            WriteHeader <= '1';
381
            FinishBlock <= '0';
382
            BlockBytes <= 0;
383
            CompressedNibbles <= 0;
384
            EndCompression <= '0';
385
            MSB <= '1';
386
            LoadROM <= '1';
387
            WAV_addr <= (others => '0');
388
            SecondsCompressed <= (others => '0');
389
            SamplesCompressed <= (others => '0');
390
            WriteState <= '0';
391
            WriteStateLSWord  <= '0';
392
            WaitADPCM := '0';
393
         end if;
394
      end if;
395
   end process;
396
 
397
   process (wb_clk_i, wb_rst_i)
398
   begin
399
      if (wb_rst_i = '1') then
400
         SamplesInFile <= (others => '0');
401
      elsif (wb_clk_i = '1' and wb_clk_i'event) then
402
         --comment next lines to save up a lot of resources (multiplier),
403
         --but then use undefined number of seconds for compression!!
404
         --(to stop it just write 0 to the MSb of WB reg 0)
405
         --just be aware that final WAV file will play but at the end,
406
         --some players will display an error saying something like "Early EOF" or "Corrupted file".
407
 
408
         --if (Compressing = '1' and EndCompression = '0') then
409
            --SamplesInFile <= SamplesPerSec1 * SecondsToCompress2;
410
         --else
411
            if (LoadROM = '1') then
412
               if (WAV_addr = "000000") then --file_size-8=SamplesInFile/2+52
413
                  --file size calculation is approximate, unless you want to mess up with floats
414
                  --or you have a fixed sampling rate (and want to modify the source code of this process)
415
                  SamplesInFile <= (('0' & SamplesPerSec1(15 downto 1)) * SecondsToCompress2) + x"34";
416
                  --SamplesInFile <= '0' & (SamplesInFile(31 downto 1) + x"34");
417
               elsif (WAV_addr = "011101") then --get raw SamplesInFile
418
                  SamplesInFile <= SamplesPerSec1 * SecondsToCompress2;
419
               elsif (WAV_addr = "110100") then --file_size-60=SamplesInFile/2
420
                  SamplesInFile <= '0' & SamplesInFile(31 downto 1);
421
               end if;
422
            end if;
423
         --end if;
424
      end if;
425
   end process;
426
end Behavioral;
427
 

powered by: WebSVN 2.1.0

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