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

Subversion Repositories mlite

[/] [mlite/] [trunk/] [vhdl/] [mlite_pack.vhd] - Blame information for rev 47

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 39 rhoads
---------------------------------------------------------------------
2 43 rhoads
-- TITLE: Plasma Misc. Package
3 39 rhoads
-- AUTHOR: Steve Rhoads (rhoadss@yahoo.com)
4
-- DATE CREATED: 2/15/01
5
-- FILENAME: mlite_pack.vhd
6 43 rhoads
-- PROJECT: Plasma CPU core
7 39 rhoads
-- COPYRIGHT: Software placed into the public domain by the author.
8
--    Software 'as is' without warranty.  Author liable for nothing.
9
-- DESCRIPTION:
10 43 rhoads
--    Data types, constants, and add functions needed for the Plasma CPU.
11 39 rhoads
---------------------------------------------------------------------
12
library ieee;
13
use ieee.std_logic_1164.all;
14
 
15
package mlite_pack is
16
   constant ZERO          : std_logic_vector(31 downto 0) :=
17
      "00000000000000000000000000000000";
18
   constant ONES          : std_logic_vector(31 downto 0) :=
19
      "11111111111111111111111111111111";
20
   --make HIGH_Z equal to ZERO if compiler complains
21
   constant HIGH_Z        : std_logic_vector(31 downto 0) :=
22
      "ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ";
23
 
24
--   type alu_function_type is (alu_nothing, alu_add, alu_subtract, 
25
--      alu_less_than, alu_less_than_signed, alu_equal, alu_not_equal,
26
--      alu_ltz, alu_lez, alu_eqz, alu_nez, alu_gez, alu_gtz,
27
--      alu_or, alu_and, alu_xor, alu_nor);
28
   subtype alu_function_type is std_logic_vector(4 downto 0);
29
   constant alu_nothing   : alu_function_type := "00000";
30
   constant alu_add       : alu_function_type := "00010";
31
   constant alu_subtract  : alu_function_type := "00011";
32
   constant alu_less_than : alu_function_type := "00100";
33
   constant alu_less_than_signed : alu_function_type := "00101";
34
   constant alu_equal     : alu_function_type := "00110";
35
   constant alu_not_equal : alu_function_type := "00111";
36
   constant alu_ltz       : alu_function_type := "01000";
37
   constant alu_lez       : alu_function_type := "01001";
38
   constant alu_eqz       : alu_function_type := "01010";
39
   constant alu_nez       : alu_function_type := "01011";
40
   constant alu_gez       : alu_function_type := "01100";
41
   constant alu_gtz       : alu_function_type := "01101";
42
   constant alu_or        : alu_function_type := "01110";
43
   constant alu_and       : alu_function_type := "01111";
44
   constant alu_xor       : alu_function_type := "10001";
45
   constant alu_nor       : alu_function_type := "10010";
46
 
47
--   type shift_function_type is (
48
--      shift_nothing, shift_left_unsigned,
49
--      shift_right_signed, do_right_unsigned);
50
   subtype shift_function_type is std_logic_vector(1 downto 0);
51
   constant shift_nothing        : shift_function_type := "00";
52
   constant shift_left_unsigned  : shift_function_type := "01";
53
   constant shift_right_signed   : shift_function_type := "11";
54
   constant shift_right_unsigned : shift_function_type := "10";
55
 
56
--   type mult_function_type is (
57
--      mult_nothing, mult_read_lo, mult_read_hi, mult_write_lo, 
58
--      mult_write_hi, mult_mult, mult_divide, mult_signed_divide);
59 44 rhoads
   subtype mult_function_type is std_logic_vector(3 downto 0);
60
   constant mult_nothing       : mult_function_type := "0000";
61
   constant mult_read_lo       : mult_function_type := "0001";
62
   constant mult_read_hi       : mult_function_type := "0010";
63
   constant mult_write_lo      : mult_function_type := "0011";
64
   constant mult_write_hi      : mult_function_type := "0100";
65
   constant mult_mult          : mult_function_type := "0101";
66
   constant mult_signed_mult   : mult_function_type := "0110";
67
   constant mult_divide        : mult_function_type := "0111";
68
   constant mult_signed_divide : mult_function_type := "1000";
69 39 rhoads
 
70
--   type a_source_type is (from_reg_source, from_imm10_6);
71
   subtype a_source_type is std_logic_vector(1 downto 0);
72
   constant a_from_reg_source : a_source_type := "00";
73
   constant a_from_imm10_6    : a_source_type := "01";
74
   constant a_from_pc         : a_source_type := "10";
75
 
76
--   type b_source_type is (from_reg_target, from_imm, from_signed_imm);
77
   subtype b_source_type is std_logic_vector(1 downto 0);
78
   constant b_from_reg_target : b_source_type := "00";
79
   constant b_from_imm        : b_source_type := "01";
80
   constant b_from_signed_imm : b_source_type := "10";
81
   constant b_from_immX4      : b_source_type := "11";
82
 
83
--   type c_source_type is (from_null, from_alu, from_shift, 
84
--      from_mult, from_memory, from_pc, from_imm_shift16,
85
--      from_reg_source_nez, from_reg_source_eqz);
86
   subtype c_source_type is std_logic_vector(2 downto 0);
87
   constant c_from_null       : c_source_type := "000";
88
   constant c_from_alu        : c_source_type := "001";
89
   constant c_from_shift      : c_source_type := "001"; --same as alu
90
   constant c_from_mult       : c_source_type := "001"; --same as alu
91
   constant c_from_memory     : c_source_type := "010";
92
   constant c_from_pc         : c_source_type := "011";
93
   constant c_from_pc_plus4   : c_source_type := "100";
94
   constant c_from_imm_shift16: c_source_type := "101";
95
   constant c_from_reg_sourcen: c_source_type := "110";
96
 
97
--   type pc_source_type is (from_inc4, from_inc8, from_reg_source, 
98
--      from_opcode25_0, from_branch, from_lbranch);
99
   subtype pc_source_type is std_logic_vector(1 downto 0);
100
   constant from_inc4       : pc_source_type := "00";
101
   constant from_opcode25_0 : pc_source_type := "01";
102
   constant from_branch     : pc_source_type := "10";
103
   constant from_lbranch    : pc_source_type := "11";
104
 
105
   subtype branch_function_type is std_logic_vector(2 downto 0);
106
   constant branch_ltz : branch_function_type := "000";
107
   constant branch_lez : branch_function_type := "001";
108
   constant branch_eq  : branch_function_type := "010";
109
   constant branch_ne  : branch_function_type := "011";
110
   constant branch_gez : branch_function_type := "100";
111
   constant branch_gtz : branch_function_type := "101";
112
   constant branch_yes : branch_function_type := "110";
113
 
114
   -- mode(32=1,16=2,8=3), signed, write
115
   subtype mem_source_type is std_logic_vector(3 downto 0);
116
   constant mem_none    : mem_source_type := "0000";
117
   constant mem_read32  : mem_source_type := "0100";
118
   constant mem_write32 : mem_source_type := "0101";
119
   constant mem_read16  : mem_source_type := "1000";
120
   constant mem_read16s : mem_source_type := "1010";
121
   constant mem_write16 : mem_source_type := "1001";
122
   constant mem_read8   : mem_source_type := "1100";
123
   constant mem_read8s  : mem_source_type := "1110";
124
   constant mem_write8  : mem_source_type := "1101";
125
 
126
   function bv_to_integer(bv: in std_logic_vector) return integer;
127
   function bv_adder(a     : in std_logic_vector(32 downto 0);
128
                     b     : in std_logic_vector(32 downto 0);
129 47 rhoads
                     do_add: in std_logic) return std_logic_vector;
130 39 rhoads
   function bv_adder_lookahead(
131
                     a     : in std_logic_vector(32 downto 0);
132
                     b     : in std_logic_vector(32 downto 0);
133 47 rhoads
                     do_add: in std_logic) return std_logic_vector;
134 39 rhoads
   function bv_negate(a : in std_logic_vector) return std_logic_vector;
135
   function bv_increment(a : in std_logic_vector(31 downto 2)
136
                     ) return std_logic_vector;
137
   function bv_inc6(a : in std_logic_vector
138
                     ) return std_logic_vector;
139 47 rhoads
 
140
   -- For Altera
141
   COMPONENT lpm_add_sub
142
      GENERIC (
143
         lpm_width          : NATURAL;
144
         lpm_direction  : STRING;
145
         lpm_type                          : STRING;
146
         lpm_hint                          : STRING);
147
      PORT (
148
         dataa    : IN STD_LOGIC_VECTOR (32 DOWNTO 0);
149
         add_sub        : IN STD_LOGIC ;
150
         datab    : IN STD_LOGIC_VECTOR (32 DOWNTO 0);
151
         result  : OUT STD_LOGIC_VECTOR (32 DOWNTO 0));
152
   END COMPONENT;
153
 
154
   -- For Altera
155
   COMPONENT lpm_ram_dp
156
      GENERIC (
157
         lpm_width        : NATURAL;
158
         lpm_widthad      : NATURAL;
159
         rden_used        : STRING;
160
         intended_device_family : STRING;
161
         lpm_indata       : STRING;
162
         lpm_wraddress_control          : STRING;
163
         lpm_rdaddress_control          : STRING;
164
         lpm_outdata      : STRING;
165
         use_eab          : STRING;
166
         lpm_type         : STRING);
167
      PORT (
168
         wren        : IN STD_LOGIC ;
169
         wrclock   : IN STD_LOGIC ;
170
         q         : OUT STD_LOGIC_VECTOR (31 DOWNTO 0);
171
         data      : IN STD_LOGIC_VECTOR (31 DOWNTO 0);
172
         rdaddress : IN STD_LOGIC_VECTOR (4 DOWNTO 0);
173
         wraddress : IN STD_LOGIC_VECTOR (4 DOWNTO 0));
174
   END COMPONENT;
175
 
176
   -- For Altera
177
   component lpm_ram_io
178
      GENERIC (
179
         intended_device_family : string;
180
         lpm_width              : natural;
181
         lpm_widthad            : natural;
182
         lpm_indata             : string := "REGISTERED";
183
         lpm_address_control    : string := "UNREGISTERED";
184
         lpm_outdata            : string := "UNREGISTERED";
185
         lpm_file               : string := "code.hex";
186
         use_eab                : string := "ON";
187
         lpm_type               : string := "LPM_RAM_DQ");
188
      PORT (
189
         outenab : in std_logic;
190
         address : in std_logic_vector(lpm_widthad-1 downto 0);
191
         inclock : in std_logic;
192
         we      : in std_logic;
193
         dio     : inout std_logic_vector(lpm_width-1 downto 0));
194
   end component; --lpm_ram_io
195
 
196
   -- For Xilinx
197
   component ramb4_s16_s16
198
      port (
199
         clka  : in std_logic;
200
         rsta  : in std_logic;
201
         addra : in std_logic_vector;
202
         dia   : in std_logic_vector;
203
         ena   : in std_logic;
204
         wea   : in std_logic;
205
         doa   : out std_logic_vector;
206
 
207
         clkb  : in std_logic;
208
         rstb  : in std_logic;
209
         addrb : in std_logic_vector;
210
         dib   : in std_logic_vector;
211
         enb   : in std_logic;
212
         web   : in std_logic);
213
   end component;
214
 
215
   component pc_next
216
      port(clk          : in std_logic;
217
           reset_in     : in std_logic;
218
           pc_new       : in std_logic_vector(31 downto 2);
219
           take_branch  : in std_logic;
220
           pause_in     : in std_logic;
221
           opcode25_0   : in std_logic_vector(25 downto 0);
222
           pc_source    : in pc_source_type;
223
           pc_out       : out std_logic_vector(31 downto 0);
224
           pc_out_plus4 : out std_logic_vector(31 downto 0));
225
   end component;
226
 
227
   component mem_ctrl
228
      port(clk          : in std_logic;
229
           reset_in     : in std_logic;
230
           pause_in     : in std_logic;
231
           nullify_op   : in std_logic;
232
           address_pc   : in std_logic_vector(31 downto 0);
233
           opcode_out   : out std_logic_vector(31 downto 0);
234
 
235
           address_data : in std_logic_vector(31 downto 0);
236
           mem_source   : in mem_source_type;
237
           data_write   : in std_logic_vector(31 downto 0);
238
           data_read    : out std_logic_vector(31 downto 0);
239
           pause_out    : out std_logic;
240
 
241
           mem_address  : out std_logic_vector(31 downto 0);
242
           mem_data_w   : out std_logic_vector(31 downto 0);
243
           mem_data_r   : in std_logic_vector(31 downto 0);
244
           mem_byte_sel : out std_logic_vector(3 downto 0);
245
           mem_write    : out std_logic;
246
           mem_pause    : in std_logic);
247
   end component;
248
 
249
   component control
250
      port(opcode       : in  std_logic_vector(31 downto 0);
251
           intr_signal  : in  std_logic;
252
           pause_in     : in  std_logic;
253
           rs_index     : out std_logic_vector(5 downto 0);
254
           rt_index     : out std_logic_vector(5 downto 0);
255
           rd_index     : out std_logic_vector(5 downto 0);
256
           imm_out      : out std_logic_vector(15 downto 0);
257
           alu_func     : out alu_function_type;
258
           shift_func   : out shift_function_type;
259
           mult_func    : out mult_function_type;
260
           branch_func  : out branch_function_type;
261
           a_source_out : out a_source_type;
262
           b_source_out : out b_source_type;
263
           c_source_out : out c_source_type;
264
           pc_source_out: out pc_source_type;
265
           mem_source_out:out mem_source_type);
266
   end component;
267
 
268
   component reg_bank
269
      generic(memory_type : string := "TRI_PORT");
270
      port(clk            : in  std_logic;
271
           reset_in       : in  std_logic;
272
           rs_index       : in  std_logic_vector(5 downto 0);
273
           rt_index       : in  std_logic_vector(5 downto 0);
274
           rd_index       : in  std_logic_vector(5 downto 0);
275
           reg_source_out : out std_logic_vector(31 downto 0);
276
           reg_target_out : out std_logic_vector(31 downto 0);
277
           reg_dest_new   : in  std_logic_vector(31 downto 0);
278
           intr_enable    : out std_logic);
279
   end component;
280
 
281
   component bus_mux
282
      port(imm_in       : in  std_logic_vector(15 downto 0);
283
           reg_source   : in  std_logic_vector(31 downto 0);
284
           a_mux        : in  a_source_type;
285
           a_out        : out std_logic_vector(31 downto 0);
286
 
287
           reg_target   : in  std_logic_vector(31 downto 0);
288
           b_mux        : in  b_source_type;
289
           b_out        : out std_logic_vector(31 downto 0);
290
 
291
           c_bus        : in  std_logic_vector(31 downto 0);
292
           c_memory     : in  std_logic_vector(31 downto 0);
293
           c_pc         : in  std_logic_vector(31 downto 0);
294
           c_pc_plus4   : in  std_logic_vector(31 downto 0);
295
           c_mux        : in  c_source_type;
296
           reg_dest_out : out std_logic_vector(31 downto 0);
297
 
298
           branch_func  : in  branch_function_type;
299
           take_branch  : out std_logic);
300
   end component;
301
 
302
   component alu
303
      generic(adder_type : string := "GENERIC");
304
      port(a_in         : in  std_logic_vector(31 downto 0);
305
           b_in         : in  std_logic_vector(31 downto 0);
306
           alu_function : in  alu_function_type;
307
           c_alu        : out std_logic_vector(31 downto 0));
308
   end component;
309
 
310
   component shifter
311
      port(value        : in  std_logic_vector(31 downto 0);
312
           shift_amount : in  std_logic_vector(4 downto 0);
313
           shift_func   : in  shift_function_type;
314
           c_shift      : out std_logic_vector(31 downto 0));
315
   end component;
316
 
317
   component mult
318
      generic(adder_type : string := "GENERIC");
319
      port(clk       : in std_logic;
320
           a, b      : in std_logic_vector(31 downto 0);
321
           mult_func : in mult_function_type;
322
           c_mult    : out std_logic_vector(31 downto 0);
323
           pause_out : out std_logic);
324
   end component;
325
 
326
   component mlite_cpu
327
      generic(memory_type : string := "ALTERA");
328
      port(clk         : in std_logic;
329
           reset_in    : in std_logic;
330
           intr_in     : in std_logic;
331
 
332
           mem_address : out std_logic_vector(31 downto 0);
333
           mem_data_w  : out std_logic_vector(31 downto 0);
334
           mem_data_r  : in std_logic_vector(31 downto 0);
335
           mem_byte_sel: out std_logic_vector(3 downto 0);
336
           mem_write   : out std_logic;
337
           mem_pause   : in std_logic);
338
   end component;
339
 
340
   -- For test bench (not synthesizable)
341
   component ram
342
      generic(load_file_name : string);
343
      port(clk          : in std_logic;
344
           mem_byte_sel : in std_logic_vector(3 downto 0);
345
           mem_write    : in std_logic;
346
           mem_address  : in std_logic_vector;
347
           mem_data_w   : in std_logic_vector(31 downto 0);
348
           mem_data_r   : out std_logic_vector(31 downto 0));
349
   end component; --ram
350
 
351
   component uart
352
      generic(save_file_name : string := "UNUSED");
353
      port(clk       : in std_logic;
354
           reset     : in std_logic;
355
           uart_sel  : in std_logic;
356
           data      : in std_logic_vector(7 downto 0);
357
           read_pin  : in std_logic;
358
           write_pin : out std_logic;
359
           pause     : out std_logic);
360
   end component; --uart
361
 
362 39 rhoads
end; --package mlite_pack
363
 
364
package body mlite_pack is
365
 
366
function add_1(a:integer) return integer is
367
begin
368
   return a+1;
369
end; --function
370
 
371
function bv_to_integer(bv: in std_logic_vector) return integer is
372
   variable result : integer;
373
   variable b      : integer;
374
begin
375
   result := 0;
376
   b := 0;
377
   for index in bv'range loop
378
      if bv(index) = '1' then
379
         b := 1;
380
      else
381
         b := 0;
382
      end if;
383
      result := result * 2 + b;
384
   end loop;
385
   return result;
386
end; --function bv_to_integer
387
 
388
function bv_adder(a     : in std_logic_vector(32 downto 0);
389
                  b     : in std_logic_vector(32 downto 0);
390 47 rhoads
                  do_add: in std_logic) return std_logic_vector is
391 39 rhoads
   variable carry_in : std_logic;
392
   variable bb       : std_logic_vector(32 downto 0);
393
   variable result   : std_logic_vector(32 downto 0);
394
begin
395 47 rhoads
   result := '0' & ZERO;
396
   if do_add = '1' then
397 39 rhoads
      bb := b;
398
      carry_in := '0';
399
   else
400
      bb := not b;
401
      carry_in := '1';
402
   end if;
403
   for index in 0 to 32 loop
404
      result(index) := a(index) xor bb(index) xor carry_in;
405
      carry_in := (carry_in and (a(index) or bb(index))) or
406
                  (a(index) and bb(index));
407
   end loop;
408
   return result;
409
end; --function
410
 
411
function bv_adder_lookahead(
412
                  a     : in std_logic_vector(32 downto 0);
413
                  b     : in std_logic_vector(32 downto 0);
414 47 rhoads
                  do_add: in std_logic) return std_logic_vector is
415 39 rhoads
   variable carry    : std_logic_vector(32 downto 0);
416
   variable p, g     : std_logic_vector(32 downto 0);
417
   variable bb       : std_logic_vector(32 downto 0);
418
   variable result   : std_logic_vector(32 downto 0);
419
   variable i        : natural;
420
begin
421 47 rhoads
   carry := '0' & ZERO;
422
   if do_add = '1' then
423 39 rhoads
      bb := b;
424
      carry(0) := '0';
425
   else
426
      bb := not b;
427
      carry(0) := '1';
428
   end if;
429
 
430
   p := a or bb;   --propogate
431
   g := a and bb;  --generate
432
   for index in 0 to 7 loop
433
      i := index*4;
434
      carry(i+1) := g(i) or
435
                    (p(i) and carry(i));
436
      i := index*4+1;
437
      carry(i+1) := g(i) or
438
                    (p(i) and g(i-1)) or
439
                    ((p(i) and p(i-1)) and carry(i-1));
440
      i := index*4+2;
441
      carry(i+1) := g(i) or
442
                    (p(i) and g(i-1)) or
443
                    (p(i) and p(i-1) and g(i-2)) or
444
                    ((p(i) and p(i-1) and p(i-2)) and carry(i-2));
445
      i := index*4+3;
446
      carry(i+1) := g(i) or
447
                    (p(i) and g(i-1)) or
448
                    (p(i) and p(i-1) and g(i-2)) or
449
                    (p(i) and p(i-1) and p(i-2) and g(i-3)) or
450
                    (((p(i) and p(i-1)) and (p(i-2) and p(i-3)))
451
                       and carry(i-3));
452
   end loop;
453
   result := (a xor bb) xor carry;
454
   return result;
455
end; --function
456
 
457
function bv_negate(a : in std_logic_vector) return std_logic_vector is
458
   variable carry_in : std_logic;
459
   variable not_a    : std_logic_vector(31 downto 0);
460
   variable result   : std_logic_vector(31 downto 0);
461
begin
462
   result := ZERO;
463
   not_a := not a;
464
   carry_in := '1';
465
   for index in a'reverse_range loop
466
      result(index) := not_a(index) xor carry_in;
467
      carry_in := carry_in and not_a(index);
468
   end loop;
469
   return result;
470
end; --function
471
 
472
function bv_increment(a : in std_logic_vector(31 downto 2)
473
                     ) return std_logic_vector is
474
   variable carry_in : std_logic;
475
   variable result   : std_logic_vector(31 downto 2);
476
begin
477 47 rhoads
   result := ZERO(31 downto 2);
478 39 rhoads
   carry_in := '1';
479
   for index in 2 to 31 loop
480
      result(index) := a(index) xor carry_in;
481
      carry_in := a(index) and carry_in;
482
   end loop;
483
   return result;
484
end; --function
485
 
486
function bv_inc6(a : in std_logic_vector
487
                     ) return std_logic_vector is
488
   variable carry_in : std_logic;
489
   variable result   : std_logic_vector(5 downto 0);
490
begin
491
   result := "000000";
492
   carry_in := '1';
493
   for index in 0 to 5 loop
494
      result(index) := a(index) xor carry_in;
495
      carry_in := a(index) and carry_in;
496
   end loop;
497
   return result;
498
end; --function
499
 
500
end; --package body
501
 
502
 

powered by: WebSVN 2.1.0

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