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

Subversion Repositories open8_urisc

[/] [open8_urisc/] [trunk/] [VHDL/] [o8_alu16.vhd] - Blame information for rev 194

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

Line No. Rev Author Line
1 193 jshamlet
-- Copyright (c)2006, 2015, 2020 Jeremy Seth Henry
2
-- All rights reserved.
3
--
4
-- Redistribution and use in source and binary forms, with or without
5
-- modification, are permitted provided that the following conditions are met:
6
--     * Redistributions of source code must retain the above copyright
7
--       notice, this list of conditions and the following disclaimer.
8
--     * Redistributions in binary form must reproduce the above copyright
9
--       notice, this list of conditions and the following disclaimer in the
10
--       documentation and/or other materials provided with the distribution,
11
--       where applicable (as part of a user interface, debugging port, etc.)
12
--
13
-- THIS SOFTWARE IS PROVIDED BY JEREMY SETH HENRY ``AS IS'' AND ANY
14
-- EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15
-- WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16
-- DISCLAIMED. IN NO EVENT SHALL JEREMY SETH HENRY BE LIABLE FOR ANY
17
-- DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18
-- (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19
-- LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
20
-- ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 194 jshamlet
-- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22
-- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 193 jshamlet
--
24
-- VHDL Units :  o8_alu16
25
-- Description:  Provides a mix of common 16-bit math functions to accelerate
26
--            :   math operations on the Open8 microprocessor core.
27
--
28
-- Notes      :  All output registers are updated by writing to offset 0x1F.
29
--
30
--            :  The math unit is busy when the MSB of the status
31
--            :   register is high, and done/ready when it reads low.
32
--            :  Almost Equal checks to see if Addend 1 is no more, or less,
33
--            :   addend 2 within the specified tolerance. For example,
34
--            :   addend_1 = 2 is almost equal to addend_2 = -1 with a
35
--            :   tolerance of 3, but not a tolerance of 1. Actual function is
36
--            :   AE = '1' when (A1 <= A2 + T) and (A1 >= A2 - T) else '0'
37
--            :   This is an inherently signed function.
38
--            : Signed Overflow/Underflow is logically equivalent to
39
--            :  S_Sum/Dif(16) xor S_Sum/Dif(15), since the apparent result
40
--            :  changes sign while the internal sign bit doesn't. For example
41
--            :  |8000| will result in (-)8000 due to the way the internal
42
--            :  logic handles sign extension. Thus, the O bit should be
43
--            :  checked when performing signed math
44
--            : Decimal Adjust converts the contents of a register into its BCD
45
--            :  equivalent. This can be used to get the base 10 representation
46
--            :  of a value for conversion to ASCII. There are two variants,
47
--            :  Byte and Word. Note that conversion times are fairly long,
48
--            :  since we are repeatedly issuing division commands, but it is
49
--            :  still faster than software emulation.
50
--            : The Byte conversion only operates on the lower 8 bits, and
51
--            :  sets the Z and N flags. The N flag is only set when SDAB is
52
--            :  used and the signed value of the register is negative. The O
53
--            :  bit is set if the upper byte of the register is non-zero, but
54
--            :  does not actually result in an calculation error.
55
--            :  Examples:
56
--            :  UDAB 0x00FE -> 0x0254, Flags -> 0x0
57
--            :  SDAB 0x00FE -> 0x0002, Flags -> 0x4
58
--            : The Word conversion uses the entire 16-bit word, and uses the
59
--            :  Flags register to hold the Tens of Thousands place. Note that
60
--            :  the N flag is still used for signed conversions, while it may
61
--            :  be used as a data bit for unsigned conversions.
62
--            :  Examples:
63
--            :  UDAW 0xD431 -> 0x4321, Flags -> 0x5 ('0', MSB, MSB-1, MSB-2)
64
--            :  SDAW 0xD431 -> 0x1216, Flags -> 0x5 ('0', N  , MSB  , MSB-1)
65
--
66
-- Register Map:
67
-- Offset  Bitfield Description                        Read/Write
68
--   0x00   AAAAAAAA Register 0 ( 7:0)                  (RW)
69
--   0x01   AAAAAAAA Register 0 (15:8)                  (RW)
70
--   0x02   AAAAAAAA Register 1 ( 7:0)                  (RW)
71
--   0x03   AAAAAAAA Register 1 (15:8)                  (RW)
72
--   0x04   AAAAAAAA Register 2 ( 7:0)                  (RW)
73
--   0x05   AAAAAAAA Register 2 (15:8)                  (RW)
74
--   0x06   AAAAAAAA Register 3 ( 7:0)                  (RW)
75
--   0x07   AAAAAAAA Register 3 (15:8)                  (RW)
76
--   0x08   AAAAAAAA Register 4 ( 7:0)                  (RW)
77
--   0x09   AAAAAAAA Register 4 (15:8)                  (RW)
78
--   0x0A   AAAAAAAA Register 5 ( 7:0)                  (RW)
79
--   0x0B   AAAAAAAA Register 5 (15:8)                  (RW)
80
--   0x0C   AAAAAAAA Register 6 ( 7:0)                  (RW)
81
--   0x0D   AAAAAAAA Register 6 (15:8)                  (RW)
82
--   0x0E   AAAAAAAA Register 7 ( 7:0)                  (RW)
83
--   0x0F   AAAAAAAA Register 7 (15:8)                  (RW)
84
--   0x10   -------- Reserved                           (--)
85
--   0x11   -------- Reserved                           (--)
86
--   0x12   -------- Reserved                           (--)
87
--   0x13   -------- Reserved                           (--)
88
--   0x14   -------- Reserved                           (--)
89
--   0x15   -------- Reserved                           (--)
90
--   0x16   -------- Reserved                           (--)
91
--   0x17   -------- Reserved                           (--)
92
--   0x18   -------- Reserved                           (--)
93
--   0x19   -------- Reserved                           (--)
94
--   0x1A   -------- Reserved                           (--)
95
--   0x1B   -------- Reserved                           (--)
96
--   0x1C   AAAAAAAA Tolerance  ( 7:0)                  (RW)
97
--   0x1D   AAAAAAAA Tolerance  (15:8)                  (RW)
98
--   0x1E   BBBBBAAA Instruction Register               (RW)
99
--                   A = Operand (register select)
100
--                   B = Opcode  (instruction select)
101
--   0x1F   E---DCBA Status & Flags                     (RW)
102
--                   A = Zero Flag
103
--                   B = Carry Flag
104
--                   C = Negative Flag
105
--                   D = Overflow / Error Flag
106
--                   E = Busy Flag (1 = busy, 0 = idle)
107
--
108
-- Instruction Map:
109
-- OP_T0X  "0000 0xxx" : Transfer R0 to Rx    R0      -> Rx (Sets Z,N)
110
-- OP_TX0  "0000 1xxx" : Transfer Rx to R0    Rx      -> R0 (Sets Z,N)
111
-- OP_CLR  "0001 0xxx" : Set Rx to 0          0x00    -> Rx (Sets Z,N)
112
--
113
-- OP_IDIV "0001 1xxx" : Integer Division     R0/Rx   -> Q:R0, R:Rx
114
--
115
-- OP_UMUL "0010 0xxx" : Unsigned Multiply    R0*Rx   -> R1:R0 (Sets Z)
116
-- OP_UADD "0010 1xxx" : Unsigned Addition    R0+Rx   -> R0 (Sets N,Z,C)
117
-- OP_UADC "0011 0xxx" : Unsigned Add w/Carry R0+Rx+C -> R0 (Sets N,Z,C)
118
-- OP_USUB "0011 1xxx" : Unsigned Subtraction R0-Rx   -> R0 (Sets N,Z,C)
119
-- OP_USBC "0100 0xxx" : Unsigned Sub w/Carry R0-Rx-C -> R0 (Sets N,Z,C)
120
-- OP_UCMP "0100 1xxx" : Unsigned Compare     R0-Rx - Sets N,Z,C only
121
--
122
-- OP_SMUL "0101 0xxx" : Signed Multiply      R0*Rx   -> R1:R0 (Sets N,Z)
123
-- OP_SADD "0101 1xxx" : Signed Addition      R0+Rx   -> R0 (Sets N,Z,O)
124
-- OP_SSUB "0110 0xxx" : Signed Subtraction   R0-Rx   -> R0 (Sets N,Z,O)
125
-- OP_SCMP "0110 1xxx" : Signed Compare       R0-Rx - Sets N,Z,O only
126
-- OP_SMAG "0111 0xxx" : Signed Magnitude     |Rx|    -> R0 (Sets Z,O)
127
-- OP_SNEG "0111 1xxx" : Signed Negation      -Rx     -> R0 (Sets N,Z,O)
128
--
129
-- OP_ACMP "1000 0xxx" : Signed Almost Equal (see description)
130
-- OP_SCRY "1000 1---" : Set the carry bit   (ignores operand)
131
--
132
-- OP_UDAB "1001 0xxx" : Decimal Adjust Byte (see description)
133
-- OP_SDAB "1001 1xxx" : Decimal Adjust Byte (see description)
134
-- OP_UDAW "1010 0xxx" : Decimal Adjust Word (see description)
135
-- OP_SDAW "1010 1xxx" : Decimal Adjust Word (see description)
136
 
137
-- OP_RSVD "1011 0---" : Reserved
138
 
139
-- OP_BSWP "1011 1xxx" : Byte Swap (Swaps upper and lower bytes)
140
 
141
-- OP_BOR  "1100 0xxx" : Bitwise Logical OR   Rx or  R0 -> R0
142
-- OP_BAND "1100 1xxx" : Bitwise Logical AND  Rx and R0 -> R0
143
-- OP_BXOR "1101 0xxx" : Bitwise Logical XOR  Rx xor R0 -> R0
144
--
145
-- OP_BINV "1101 1xxx" : Bitwise logical NOT #Rx      -> Rx
146
-- OP_BSFL "1110 0xxx" : Logical Shift Left   Rx<<1,0 -> Rx
147
-- OP_BROL "1110 1xxx" : Logical Rotate Left  Rx<<1,C -> Rx,C
148
-- OP_BSFR "1111 0xxx" : Logical Shift Right  0,Rx>>1 -> Rx
149
-- OP_BROR "1111 1xxx" : Logical Rotate Right C,Rx>>1 -> Rx,C
150
--
151
-- Revision History
152
-- Author          Date     Change
153
------------------ -------- --------------------------------------------------
154
-- Seth Henry      07/19/06 Design Start
155
-- Seth Henry      03/13/15 Added "Almost Equal" instruction
156
-- Seth Henry      12/19/19 Renamed to o8_alu16 to fit "theme"
157
 
158
library ieee;
159
use ieee.std_logic_1164.all;
160
use ieee.std_logic_arith.all;
161
use ieee.std_logic_unsigned.all;
162
use ieee.std_logic_misc.all;
163
 
164
library work;
165
  use work.open8_pkg.all;
166
 
167
entity o8_alu16 is
168
generic(
169
  Reset_Level           : std_logic;
170
  Address               : ADDRESS_TYPE
171
);
172
port(
173
  Clock                 : in  std_logic;
174
  Reset                 : in  std_logic;
175
  --
176
  Bus_Address           : in  ADDRESS_TYPE;
177
  Wr_Enable             : in  std_logic;
178
  Wr_Data               : in  DATA_TYPE;
179
  Rd_Enable             : in  std_logic;
180
  Rd_Data               : out DATA_TYPE;
181
  Interrupt             : out std_logic
182
);
183
end entity;
184
 
185
architecture behave of o8_alu16 is
186
 
187
  -------------------------------------------------------------------
188
  -- Opcode Definitions (should match the table above)
189
  -- Register Manipulation
190
  constant OP_T0X       : std_logic_vector(4 downto 0) := "00000";
191
  constant OP_TX0       : std_logic_vector(4 downto 0) := "00001";
192
  constant OP_CLR       : std_logic_vector(4 downto 0) := "00010";
193
 
194
  -- Integer Division
195
  constant OP_IDIV      : std_logic_vector(4 downto 0) := "00011";
196
 
197
  -- Unsigned Math Operations
198
  constant OP_UMUL      : std_logic_vector(4 downto 0) := "00100";
199
  constant OP_UADD      : std_logic_vector(4 downto 0) := "00101";
200
  constant OP_UADC      : std_logic_vector(4 downto 0) := "00110";
201
  constant OP_USUB      : std_logic_vector(4 downto 0) := "00111";
202
  constant OP_USBC      : std_logic_vector(4 downto 0) := "01000";
203
  constant OP_UCMP      : std_logic_vector(4 downto 0) := "01001";
204
 
205
  -- Signed Math Operations
206
  constant OP_SMUL      : std_logic_vector(4 downto 0) := "01010";
207
  constant OP_SADD      : std_logic_vector(4 downto 0) := "01011";
208
  constant OP_SSUB      : std_logic_vector(4 downto 0) := "01100";
209
  constant OP_SCMP      : std_logic_vector(4 downto 0) := "01101";
210
  constant OP_SMAG      : std_logic_vector(4 downto 0) := "01110";
211
  constant OP_SNEG      : std_logic_vector(4 downto 0) := "01111";
212
 
213
  -- Signed Almost Equal
214
  constant OP_ACMP      : std_logic_vector(4 downto 0) := "10000";
215
 
216
  -- Carry Flag set/clear
217
  constant OP_SCRY      : std_logic_vector(4 downto 0) := "10001";
218
 
219
  -- (Un)Signed Decimal Adjust Byte
220
  constant OP_UDAB      : std_logic_vector(4 downto 0) := "10010";
221
  constant OP_SDAB      : std_logic_vector(4 downto 0) := "10011";
222
 
223
  -- (Un)Signed Decimal Adjust Word
224
  constant OP_UDAW      : std_logic_vector(4 downto 0) := "10100";
225
  constant OP_SDAW      : std_logic_vector(4 downto 0) := "10101";
226
 
227
  -- Reserved for future use
228
  constant OP_RSVD      : std_logic_vector(4 downto 0) := "10110";
229
 
230
  -- Byte Swap ( U <> L )
231
  constant OP_BSWP      : std_logic_vector(4 downto 0) := "10111";
232
 
233
  -- Bitwise Boolean Operations (two operand)
234
  constant OP_BOR       : std_logic_vector(4 downto 0) := "11000";
235
  constant OP_BAND      : std_logic_vector(4 downto 0) := "11001";
236
  constant OP_BXOR      : std_logic_vector(4 downto 0) := "11010";
237
 
238
  -- In-place Bitwise Boolean Operations (single operand)
239
  constant OP_BINV      : std_logic_vector(4 downto 0) := "11011";
240
  constant OP_BSFL      : std_logic_vector(4 downto 0) := "11100";
241
  constant OP_BROL      : std_logic_vector(4 downto 0) := "11101";
242
  constant OP_BSFR      : std_logic_vector(4 downto 0) := "11110";
243
  constant OP_BROR      : std_logic_vector(4 downto 0) := "11111";
244
  -------------------------------------------------------------------
245
 
246
  constant User_Addr    : std_logic_vector(15 downto 5):=
247
                            Address(15 downto 5);
248
  alias Comp_Addr       is Bus_Address(15 downto 5);
249
  signal Reg_Addr       : std_logic_vector(4 downto 0);
250
 
251
  signal Addr_Match     : std_logic;
252
  signal Wr_En          : std_logic;
253
  signal Wr_Data_q      : DATA_TYPE;
254
  signal Rd_En          : std_logic;
255
 
256
  type REG_ARRAY is array( 0 to 7 ) of std_logic_vector(15 downto 0);
257
  signal regfile        : REG_ARRAY;
258
 
259
  signal Start          : std_logic;
260
  signal Opcode         : std_logic_vector(4 downto 0);
261
  signal Operand_Sel    : std_logic_vector(2 downto 0);
262
 
263
  signal Tolerance      : std_logic_vector(15 downto 0);
264
  signal High_Tol       : signed(16 downto 0);
265
  signal Low_Tol        : signed(16 downto 0);
266
  signal Almost_Equal   : std_logic;
267
 
268
  constant FLAG_Z       : integer := 0;
269
  constant FLAG_C       : integer := 1;
270
  constant FLAG_N       : integer := 2;
271
  constant FLAG_O       : integer := 3;
272
 
273
  signal Flags          : std_logic_vector(3 downto 0);
274
 
275
  type ALU_STATES is ( IDLE, LOAD, EXECUTE,   IDIV_INIT, IDIV_WAIT,
276
                       DAW_INIT,   DAB_INIT,  DAA_WAIT1, DAA_STEP2,
277
                       DAA_WAIT2,  DAA_STEP3, DAA_WAIT3, DAA_STEP4,
278
                       STORE );
279
  signal alu_ctrl       : ALU_STATES;
280
 
281
  signal Busy           : std_logic;
282
  signal Busy_q         : std_logic;
283
 
284
  signal Operand_1      : std_logic_vector(15 downto 0);
285
  signal Operand_2      : std_logic_vector(15 downto 0);
286
 
287
  alias  Dividend       is Operand_1;
288
  alias  Divisor        is Operand_2;
289
 
290
  alias  u_Operand_1    is Operand_1;
291
  alias  u_Operand_2    is Operand_2;
292
 
293
  alias  u_Addend_1     is Operand_1;
294
  alias  u_Addend_2     is Operand_2;
295
 
296
  signal s_Operand_1    : signed(16 downto 0);
297
  signal s_Operand_2    : signed(16 downto 0);
298
 
299
  alias  s_Addend_1     is S_Operand_1;
300
  alias  s_Addend_2     is S_Operand_2;
301
 
302
  signal u_accum        : std_logic_vector(16 downto 0);
303
  alias  u_data         is u_accum(15 downto 0);
304
  alias  u_sign         is u_accum(15);
305
  alias  u_carry        is u_accum(16);
306
 
307
  signal u_prod         : std_logic_vector(31 downto 0);
308
 
309
  signal s_accum        : signed(16 downto 0);
310
  alias  s_data         is s_accum(15 downto 0);
311
  alias  s_sign         is s_accum(15);
312
  alias  s_ovf          is s_accum(16);
313
 
314
  signal s_prod         : signed(33 downto 0);
315
 
316
  signal IDIV_Start     : std_logic;
317
  signal IDIV_Busy      : std_logic;
318
 
319
  constant DIV_WIDTH    : integer := 16; -- Width of Operands
320
 
321
  signal q              : std_logic_vector(DIV_WIDTH*2-1 downto 0);
322
  signal diff           : std_logic_vector(DIV_WIDTH downto 0);
323
  signal count          : integer range 0 to DIV_WIDTH + 1;
324
 
325
  signal Quotient_i     : std_logic_vector(15 downto 0);
326
  signal Quotient       : std_logic_vector(15 downto 0);
327
 
328
  signal Remainder_i    : std_logic_vector(15 downto 0);
329
  signal Remainder      : std_logic_vector(15 downto 0);
330
 
331
  signal DAA_intreg     : std_logic_vector(15 downto 0);
332
  signal DAA_mode       : std_logic;
333
  signal DAA_sign       : std_logic;
334
  signal DAA_p4         : std_logic_vector(3 downto 0);
335
  signal DAA_p3         : std_logic_vector(3 downto 0);
336
  signal DAA_p2         : std_logic_vector(3 downto 0);
337
  alias  DAA_p1         is Quotient(3 downto 0);
338
  alias  DAA_p0         is Remainder(3 downto 0);
339
  signal DAA_result     : std_logic_vector(19 downto 0);
340
 
341
begin
342
 
343
  Addr_Match            <= '1' when Comp_Addr = User_Addr else '0';
344
 
345
  -- Sign-extend the base operands to created operands for signed math
346
  S_Operand_1           <= signed(Operand_1(15) & Operand_1);
347
  S_Operand_2           <= signed(Operand_2(15) & Operand_2);
348
 
349
  -- Compute the tolerance bounds for the Almost Equal function
350
  High_Tol              <= S_Operand_2 + signed('0' & Tolerance);
351
  Low_Tol               <= S_Operand_2 - signed('0' & Tolerance);
352
 
353
  -- Combinational logic for the Decimal Adjust logic
354
  DAA_result            <= DAA_p4 & DAA_p3 & DAA_p2 & DAA_p1 & DAA_p0;
355
 
356
  -- Combinational logic for the division logic
357
  diff                  <= ('0' & Q(DIV_WIDTH*2-2 downto DIV_WIDTH-1)) -
358
                           ('0' & Divisor);
359
  Quotient_i            <= q(DIV_WIDTH-1 downto 0);
360
  Remainder_i           <= q(DIV_WIDTH*2-1 downto DIV_WIDTH);
361
 
362
  ALU_proc: process( Clock, Reset )
363
    variable Reg_Sel    : integer;
364
    variable Oper_Sel   : integer;
365
  begin
366
    if( Reset = Reset_Level )then
367
      Wr_En             <= '0';
368
      Wr_Data_q         <= (others => '0');
369
      Rd_En             <= '0';
370
      Rd_Data           <= OPEN8_NULLBUS;
371
      Reg_Addr          <= (others => '0');
372
      Opcode            <= (others => '0');
373
      Operand_Sel       <= (others => '0');
374
      Tolerance         <= (others => '0');
375
      Start             <= '0';
376
      Busy_q            <= '0';
377
      Interrupt         <= '0';
378
      for i in 0 to 7 loop
379
        regfile(i)      <= (others => '0');
380
      end loop;
381
      alu_ctrl          <= IDLE;
382
      Operand_1         <= (others => '0');
383
      Operand_2         <= (others => '0');
384
      u_accum           <= (others => '0');
385
      u_prod            <= (others => '0');
386
      s_accum           <= (others => '0');
387
      s_prod            <= (others => '0');
388
      Quotient          <= (others => '0');
389
      Remainder         <= (others => '0');
390
      Flags             <= (others => '0');
391
      Almost_Equal      <= '0';
392
      Busy              <= '0';
393
      DAA_mode          <= '0';
394
      DAA_sign          <= '0';
395
      DAA_intreg        <= (others => '0');
396
      DAA_p4            <= (others => '0');
397
      DAA_p3            <= (others => '0');
398
      DAA_p2            <= (others => '0');
399
      IDIV_Start        <= '0';
400
      q                 <= (others => '0');
401
      count             <= DIV_WIDTH;
402
      IDIV_Busy         <= '0';
403
    elsif( rising_edge(Clock) )then
404
      -- For convenience, convert these to integers and assign them to
405
      --  variables
406
      Reg_Sel           := conv_integer(Reg_Addr(3 downto 1));
407
      Oper_Sel          := conv_integer(Operand_Sel);
408
 
409
      Wr_En             <= Addr_Match and Wr_Enable;
410
      Wr_Data_q         <= Wr_Data;
411
      Reg_Addr          <= Bus_Address(4 downto 0);
412
 
413
      Start             <= '0';
414
      if( Wr_En = '1' )then
415
        case( Reg_Addr )is
416
          -- Even addresses go to the lower byte of the register
417
          when "00000" | "00010" | "00100" | "00110" |
418
               "01000" | "01010" | "01100" | "01110" =>
419
            regfile(Reg_Sel)(7 downto 0) <= Wr_Data_q;
420
 
421
          -- Odd addresses go to the upper byte of the register
422
          when "00001" | "00011" | "00101" | "00111" |
423
               "01001" | "01011" | "01101" | "01111" =>
424
            regfile(Reg_Sel)(15 downto 8)<= Wr_Data_q;
425
 
426
          when "11100" => -- 0x1C -> Tolerance.l
427
            Tolerance(7 downto 0) <= Wr_Data_q;
428
 
429
          when "11101" => -- 0x1D -> Tolerance.u
430
            Tolerance(15 downto 8) <= Wr_Data_q;
431
 
432
          when "11110" => -- 0x1E -> Opcode register
433
            Opcode      <= Wr_Data_q(7 downto 3);
434
            Operand_Sel <= Wr_Data_q(2 downto 0);
435
 
436
          when "11111" => -- 0x1F -> Status/Start register
437
            Start       <= '1';
438
 
439
          when others => null;
440
        end case;
441
      end if;
442
 
443
      Rd_Data           <= OPEN8_NULLBUS;
444
      Rd_En             <= Addr_Match and Rd_Enable;
445
 
446
      if( Rd_En = '1' )then
447
        case( Reg_Addr )is
448
          when "00000" | "00010" | "00100" | "00110" |
449
               "01000" | "01010" | "01100" | "01110" =>
450
            Rd_Data  <= regfile(Reg_Sel)(7 downto 0);
451
          when "00001" | "00011" | "00101" | "00111" |
452
               "01001" | "01011" | "01101" | "01111" =>
453
            Rd_Data  <= regfile(Reg_Sel)(15 downto 8);
454
          when "11100" => -- 0x1C -> Tolerance.l
455
            Rd_Data  <= Tolerance(7 downto 0);
456
          when "11101" => -- 0x1D -> Tolerance.u
457
            Rd_Data  <= Tolerance(15 downto 8);
458
          when "11110" => -- 0x1E -> Opcode register
459
            Rd_Data  <= Opcode & Operand_Sel;
460
          when "11111" => -- 0x1F -> Flags & Status register
461
            Rd_Data  <= Busy & "000" & Flags;
462
 
463
          when others => null;
464
        end case;
465
      end if;
466
 
467
      Busy              <= '1';
468
      IDIV_Start        <= '0';
469
      case( alu_ctrl )is
470
        when IDLE =>
471
          Busy          <= '0';
472
          if( Start = '1' )then
473
            alu_ctrl    <= LOAD;
474
          end if;
475
 
476
        -- Load the operands from the reg file. We also check for specific
477
        --  opcodes to set the DAA mode (signed vs unsigned). This is the only
478
        --  place where we READ the register file outside of the bus interface
479
        when LOAD =>
480
          Operand_1     <= regfile(0);
481
          Operand_2     <= regfile(Oper_Sel);
482
          DAA_mode      <= '0';
483
          if( Opcode = OP_SDAW or Opcode = OP_SDAB )then
484
            DAA_mode    <= '1';
485
          end if;
486
          alu_ctrl      <= EXECUTE;
487
 
488
        -- Now that the operands are loaded, we can execute the actual math
489
        --  operations. We do it with separate operand registers to pipeline
490
        --  the logic.
491
        when EXECUTE =>
492
          alu_ctrl      <= STORE;
493
          case( Opcode)is
494
            when OP_T0X =>
495
              u_accum   <= '0' & Operand_1;
496
 
497
            when OP_TX0 =>
498
              u_accum   <= '0' & Operand_2;
499
 
500
            when OP_CLR | OP_SCRY =>
501
              u_accum   <= (others => '0');
502
 
503
            when OP_BSWP =>
504
              u_accum   <= '0' &
505
                           Operand_2(7 downto 0) &
506
                           Operand_2(15 downto 8);
507
 
508
            when OP_SMAG =>
509
              s_accum   <= S_Operand_2;
510
              if( S_Operand_2 < 0)then
511
                s_accum <= -S_Operand_2;
512
              end if;
513
 
514
            when OP_SNEG =>
515
              s_accum   <= -S_Operand_2;
516
 
517
            when OP_SMUL =>
518
              s_prod    <= S_Operand_1 * S_Operand_2;
519
 
520
            when OP_UMUL =>
521
              u_prod    <= U_Operand_1 * U_Operand_2;
522
 
523
            when OP_SADD =>
524
              s_accum   <= S_Addend_1  + S_Addend_2;
525
 
526
            when OP_UADD =>
527
              u_accum   <= ('0' & Operand_1) +
528
                           ('0' & Operand_2);
529
 
530
            when OP_UADC =>
531
              u_accum   <= ('0' & Operand_1) +
532
                           ('0' & Operand_2) +
533
                           Flags(FLAG_C);
534
 
535
            when OP_SSUB | OP_SCMP =>
536
              s_accum   <= S_Addend_1 - S_Addend_2;
537
 
538
            when OP_USUB | OP_UCMP =>
539
              u_accum   <= ('0' & U_Addend_1) -
540
                           ('0' & U_Addend_2);
541
 
542
            when OP_USBC =>
543
              u_accum   <= ('0' & U_Addend_1) -
544
                           ('0' & U_Addend_2) -
545
                           Flags(FLAG_C);
546
 
547
            when OP_ACMP =>
548
              -- Perform the function
549
              -- AE = '1' when (A1 <= A2 + T) and (A1 >= A2 - T) else '0'
550
              Almost_Equal    <= '0';
551
              if( (S_Addend_1 <= High_Tol) and
552
                  (S_Addend_1 >= Low_Tol) )then
553
                Almost_Equal  <= '1';
554
              end if;
555
 
556
            when OP_BINV =>
557
              u_accum    <= '0' & (not U_Operand_1);
558
 
559
            when OP_BSFL =>
560
              u_accum    <= U_Operand_1 & '0';
561
 
562
            when OP_BROL =>
563
              u_accum    <= U_Operand_1 & Flags(FLAG_C);
564
 
565
            when OP_BSFR =>
566
              u_accum    <= "00" & U_Operand_1(15 downto 1);
567
 
568
            when OP_BROR =>
569
              u_accum    <= U_Operand_1(0) & Flags(FLAG_C) &
570
                            U_Operand_1(15 downto 1);
571
 
572
            when OP_BOR  =>
573
              u_accum    <= '0' & (U_Operand_1 or U_Operand_2);
574
 
575
            when OP_BAND =>
576
              u_accum    <= '0' & (U_Operand_1 and U_Operand_2);
577
 
578
            when OP_BXOR =>
579
              u_accum    <= '0' & (U_Operand_1 xor U_Operand_2);
580
 
581
        -- Division unit has a longer latency, so we need to wait for its busy
582
        --  signal to return low before storing results. Trigger the engine,
583
        --  and then jump to the wait state for it to finish
584
            when OP_IDIV =>
585
              IDIV_Start<= '1';
586
              alu_ctrl  <= IDIV_INIT;
587
 
588
        -- Decimal Adjust Word initialization
589
        --  Stores the sign bit for later use setting the N flag
590
        --  Assigns Operand_1 to register as-is
591
        --  If the sign bit is set, do a 2's complement of the register
592
            when OP_UDAW | OP_SDAW =>
593
              IDIV_Start<= '1';
594
              DAA_sign  <= Operand_2(15);
595
              Operand_1 <= Operand_2;
596
              if( (Operand_2(15) and DAA_mode) = '1' )then
597
                Operand_1 <= (not Operand_2) + 1;
598
              end if;
599
              Operand_2 <= x"2710";
600
              alu_ctrl  <= DAW_INIT;
601
 
602
        -- Decimal Adjust Byte initialization
603
        --  Stores the sign bit for later use setting the N flag
604
        --  Assigns Operand_1 to the lower byte of the register
605
        --  If the sign bit is set, do a 2's complement of the register
606
            when OP_UDAB | OP_SDAB =>
607
              IDIV_Start<= '1';
608
              DAA_p4    <= (others => '0');
609
              DAA_p3    <= (others => '0');
610
              DAA_sign  <= Operand_2(7);
611
              Operand_1 <= x"00" & Operand_2(7 downto 0);
612
              if( (Operand_2(7) and DAA_mode) = '1' )then
613
                Operand_1 <= ((not Operand_2) + 1) and x"00FF";
614
              end if;
615
              Operand_2 <= x"0064";
616
              alu_ctrl  <= DAB_INIT;
617
 
618
            when others => null;
619
          end case;
620
 
621
        -- These three states look superfluous, but simplify the state machine
622
        --  logic enough to improve performance. Leave them.
623
        when IDIV_INIT =>
624
          if( IDIV_Busy = '1' )then
625
            alu_ctrl    <= IDIV_WAIT;
626
          end if;
627
 
628
        when DAW_INIT =>
629
          if( IDIV_Busy = '1' )then
630
            alu_ctrl    <= DAA_WAIT1;
631
          end if;
632
 
633
        when DAB_INIT =>
634
          if( IDIV_Busy = '1' )then
635
            alu_ctrl    <= DAA_WAIT3;
636
          end if;
637
 
638
        when DAA_WAIT1 =>
639
          if( IDIV_Busy = '0' )then
640
            DAA_p4      <= Quotient_i(3 downto 0);
641
            DAA_intreg  <= Remainder_i;
642
            alu_ctrl    <= DAA_STEP2;
643
          end if;
644
 
645
        when DAA_STEP2 =>
646
          Operand_1     <= DAA_intreg;
647
          Operand_2     <= x"03E8";
648
          IDIV_Start    <= '1';
649
          if( IDIV_Busy = '1' )then
650
            alu_ctrl    <= DAA_WAIT2;
651
          end if;
652
 
653
        when DAA_WAIT2 =>
654
          if( IDIV_Busy = '0' )then
655
            DAA_p3      <= Quotient_i(3 downto 0);
656
            DAA_intreg  <= Remainder_i;
657
            alu_ctrl    <= DAA_STEP3;
658
          end if;
659
 
660
        when DAA_STEP3 =>
661
          Operand_1     <= DAA_intreg;
662
          Operand_2     <= x"0064";
663
          IDIV_Start    <= '1';
664
          if( IDIV_Busy = '1' )then
665
            alu_ctrl    <= DAA_WAIT3;
666
          end if;
667
 
668
        when DAA_WAIT3 =>
669
          if( IDIV_Busy = '0' )then
670
            DAA_p2      <= Quotient_i(3 downto 0);
671
            DAA_intreg  <= Remainder_i;
672
            alu_ctrl    <= DAA_STEP4;
673
          end if;
674
 
675
        when DAA_STEP4 =>
676
          Operand_1     <= DAA_intreg;
677
          Operand_2     <= x"000A";
678
          IDIV_Start    <= '1';
679
          if( IDIV_Busy = '1' )then
680
            alu_ctrl    <= IDIV_WAIT;
681
          end if;
682
 
683
        when IDIV_WAIT =>
684
          if( IDIV_Busy = '0' )then
685
            Quotient    <= Quotient_i;
686
            Remainder   <= Remainder_i;
687
            alu_ctrl    <= STORE;
688
          end if;
689
 
690
        -- All ALU writes to the register file go through here. This is also
691
        --  where the flag register gets updated. This should be the only
692
        --  place where the register file gets WRITTEN outside of the bus
693
        --  interface.
694
        when STORE =>
695
          Flags          <= (others => '0');
696
          case( Opcode)is
697
            when OP_T0X | OP_CLR | OP_BSWP =>
698
              regfile(Oper_Sel) <= u_data;
699
              Flags(FLAG_Z) <= nor_reduce(u_data);
700
              Flags(FLAG_N) <= u_sign;
701
 
702
            when OP_TX0  =>
703
              regfile(0) <= u_data;
704
              Flags(FLAG_Z) <= nor_reduce(u_data);
705
              Flags(FLAG_N) <= u_sign;
706
 
707
            when OP_SCRY =>
708
              Flags(FLAG_C) <= '0';
709
              if( Oper_Sel > 0 )then
710
                Flags(FLAG_C)<= '1';
711
              end if;
712
 
713
            when OP_IDIV =>
714
              regfile(0) <= Quotient;
715
              regfile(Oper_Sel) <= Remainder;
716
              Flags(FLAG_Z) <= nor_reduce(Quotient);
717
 
718
            when OP_SMAG | OP_SNEG | OP_SADD | OP_SSUB =>
719
              regfile(0) <= std_logic_vector(s_data);
720
              Flags(FLAG_N) <= s_sign;
721
              Flags(FLAG_Z) <= nor_reduce(std_logic_vector(s_data));
722
              Flags(FLAG_O) <= s_ovf xor s_sign;
723
 
724
            when OP_SMUL =>
725
              regfile(0) <= std_logic_vector(s_prod(15 downto 0));
726
              regfile(1) <= std_logic_vector(s_prod(31 downto 16));
727
              Flags(FLAG_N) <= s_prod(33) or s_prod(32);
728
              Flags(FLAG_Z) <= nor_reduce(std_logic_vector(s_prod));
729
 
730
            when OP_UMUL =>
731
              regfile(0) <= u_prod(15 downto 0);
732
              regfile(1) <= u_prod(31 downto 16);
733
              Flags(FLAG_N) <= u_prod(31);
734
              Flags(FLAG_Z) <= nor_reduce(u_prod);
735
 
736
            when OP_UADD | OP_USUB =>
737
              regfile(0) <= u_data;
738
              Flags(FLAG_Z) <= nor_reduce(u_data);
739
              Flags(FLAG_N) <= u_sign;
740
              Flags(FLAG_C) <= u_carry;
741
 
742
            when OP_SCMP =>
743
              Flags(FLAG_N) <= s_ovf;
744
              Flags(FLAG_Z) <= nor_reduce(std_logic_vector(s_data));
745
              Flags(FLAG_O) <= s_accum(16) xor s_accum(15);
746
 
747
            when OP_UCMP =>
748
              Flags(FLAG_Z) <= nor_reduce(u_data);
749
              Flags(FLAG_C) <= u_carry;
750
 
751
            when OP_ACMP =>
752
              Flags(FLAG_Z) <= Almost_Equal;
753
 
754
            when OP_UDAB | OP_SDAB =>
755
              regfile(Oper_Sel) <= DAA_result(15 downto 0);
756
              Flags(FLAG_Z) <= nor_reduce(DAA_result);
757
              Flags(FLAG_N) <= DAA_sign;
758
 
759
            when OP_UDAW | OP_SDAW =>
760
              regfile(Oper_Sel) <= DAA_result(15 downto 0);
761
              Flags(3 downto 0) <= DAA_result(19 downto 16);
762
              if( DAA_mode = '1' )then
763
                Flags(FLAG_N) <= DAA_sign;
764
              end if;
765
 
766
            when OP_BOR  | OP_BAND | OP_BXOR =>
767
              regfile(0) <= u_data;
768
              Flags(FLAG_Z) <= nor_reduce(u_data);
769
              Flags(FLAG_N) <= u_sign;
770
 
771
            when OP_BINV =>
772
              regfile(Oper_Sel) <= u_data;
773
              Flags(FLAG_Z) <= nor_reduce(u_data);
774
              Flags(FLAG_N) <= u_sign;
775
 
776
            when OP_BSFL | OP_BROL | OP_BSFR | OP_BROR =>
777
              regfile(Oper_Sel) <= u_data;
778
              Flags(FLAG_Z) <= nor_reduce(u_data);
779
              Flags(FLAG_N) <= u_sign;
780
              Flags(FLAG_C) <= u_carry;
781
 
782
            when others => null;
783
          end case;
784
          alu_ctrl      <= IDLE;
785
 
786
        when others =>
787
          null;
788
 
789
      end case;
790
 
791
      IDIV_Busy         <= '0';
792
      if( IDIV_Start = '1' )then
793
        IDIV_Busy       <= '1';
794
        count           <= 0;
795
        q               <= conv_std_logic_vector(0,DIV_WIDTH) & Dividend;
796
      elsif( count < DIV_WIDTH )then
797
        IDIV_Busy       <= '1';
798
        count           <= count + 1;
799
        q               <= diff(DIV_WIDTH-1 downto 0) &
800
                         q(DIV_WIDTH-2 downto 0) &
801
                  '1';
802
        if( diff(DIV_WIDTH) = '1' )then
803
          q             <= q(DIV_WIDTH*2-2 downto 0) & '0';
804
        end if;
805
      end if;
806
 
807
      -- Fire on the falling edge of Busy
808
      Busy_q            <= Busy;
809
      Interrupt         <= not Busy and Busy_q;
810
 
811
    end if;
812
  end process;
813
 
814
end architecture;

powered by: WebSVN 2.1.0

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