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

Subversion Repositories rv01_riscv_core

[/] [rv01_riscv_core/] [trunk/] [VHDL/] [RV01_csru.vhd] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 madsilicon
-----------------------------------------------------------------
2
--                                                             --
3
-----------------------------------------------------------------
4
--                                                             --
5
-- Copyright (C) 2015 Stefano Tonello                          --
6
--                                                             --
7
-- This source file may be used and distributed without        --
8
-- restriction provided that this copyright statement is not   --
9
-- removed from the file and that any derivative work contains --
10
-- the original copyright notice and the associated disclaimer.--
11
--                                                             --
12
-- THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY         --
13
-- EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED   --
14
-- TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS   --
15
-- FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL THE AUTHOR      --
16
-- OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,         --
17
-- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES    --
18
-- (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE   --
19
-- GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR        --
20
-- BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF  --
21
-- LIABILITY, WHETHER IN  CONTRACT, STRICT LIABILITY, OR TORT  --
22
-- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT  --
23
-- OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE         --
24
-- POSSIBILITY OF SUCH DAMAGE.                                 --
25
--                                                             --
26
-----------------------------------------------------------------
27
 
28
---------------------------------------------------------------
29
-- RV01 CSR's unit
30
---------------------------------------------------------------
31
 
32
library IEEE;
33
use IEEE.std_logic_1164.all;
34
use IEEE.numeric_std.all;
35
 
36
library WORK;
37
use WORK.RV01_CONSTS_PKG.all;
38
use WORK.RV01_TYPES_PKG.all;
39
use WORK.RV01_ARITH_PKG.all;
40
use WORK.RV01_FUNCS_PKG.all;
41
use WORK.RV01_OP_PKG.all;
42
use WORK.RV01_CSR_PKG.all;
43
--use WORK.RV01_CFG_PKG.all;
44
 
45
entity RV01_CSRU is
46
  generic(
47
    PXE : std_logic := '1';
48
    FPU_PRESENT : std_logic := '0';
49
    NW : natural := 2
50
  );
51
  port(
52
    CLK_i : in std_logic;
53
    RST_i : in std_logic;
54
    IX1_V0_i : in std_logic;
55
    CS_OP_i : in CS_OP_T;
56
    RS1_i : in RID_T;
57
    ADR_i : in signed(12-1 downto 0);
58
    WE_i : in std_logic;
59
    CSRD_i : in SDWORD_T;
60
    EXCP_i : in std_logic;
61
    EPC_i : in ADR_T;
62
    ECAUSE_i : in std_logic_vector(5-1 downto 0);
63
    EBADR_i : in ADR_T;
64
    ERET_i : in std_logic;
65
    IX3_V_i : in std_logic_vector(NW-1 downto 0);
66
    NOPR_i : in std_logic;
67
    HALT_i : in std_logic;
68
    STOPCYCLE_i : in std_logic;
69
    STOPTIME_i : in std_logic;
70
    --
71
    MFROMHOST_WE_i : in std_logic;
72
    MFROMHOST_i : in std_logic_vector(SDLEN-1 downto 0);
73
    -- Debug interface
74
    DMODE_i : in std_logic; -- debug mode flag
75
    DIE_i : in std_logic; -- debug int. enable flag
76
    -- Control port
77
    CPRE_i : in std_logic;
78
    CPWE_i : in std_logic;
79
    CPADR_i : in std_logic_vector(17-1 downto 0);
80
    CPD_i : in std_logic_vector(SDLEN-1 downto 0);
81
 
82
    PXE_o : out std_logic;
83
    MSTATUS_o : out SDWORD_T;
84
    MEPC_o : out ADR_T;
85
    MBASE_o : out ADR_T;
86
    MBOUND_o : out ADR_T;
87
    MIBASE_o : out ADR_T;
88
    MIBOUND_o : out ADR_T;
89
    MDBASE_o : out ADR_T;
90
    MDBOUND_o : out ADR_T;
91
    --RSTVA_o : out ADR_T;
92
    ETVA_o : out ADR_T;
93
    MTOHOST_o : out std_logic_vector(SDLEN-1 downto 0);
94
    MTOHOST_OE_o : out std_logic;
95
    ILLG_o : out std_logic;
96
    SFT_INT_o : out std_logic;
97
    TMR_INT_o : out std_logic;
98
    FFLAGS_o : out std_logic_vector(5-1 downto 0);
99
    FRM_o : out std_logic_vector(3-1 downto 0);
100
    IE_o : out std_logic;
101
    CSRQ_o : out SDWORD_T;
102
    -- Control port
103
    CPQ_o : out std_logic_vector(SDLEN-1 downto 0)
104
  );
105
end RV01_CSRU;
106
 
107
architecture ARC of RV01_CSRU is
108
 
109
  constant MAX_CNT : natural := 2147483647; --2**(SDLEN-1)-1;
110
 
111
  constant MSIP : natural := 3;
112
  constant MTIP : natural := 7;
113
  constant MSIE : natural := 3;
114
  constant MTIE : natural := 7;
115
 
116
  signal CS_OP_q : CS_OP_T;
117
  signal CS_OP_V : std_logic;
118
  signal CSRD_q,ICSRD : SDWORD_T;
119
  signal RS1_q : RID_T;
120
  signal WE_q,IWE : std_logic;
121
  signal ADR_q : signed(12-1 downto 0);
122
  signal MSTATUS_q : SDWORD_T;
123
  signal MTVEC_q : SDWORD_T;
124
  signal MIE_q : SDWORD_T;
125
  --signal MTIMECMP_q : SDWORD_T;
126
  signal MTIMECMP_q : natural range 0 to MAX_CNT;
127
  signal MTIME,MTIME_q,MTIME_q2 : natural range 0 to MAX_CNT;
128
  signal MTIMEH,MTIMEH_q : natural range 0 to MAX_CNT;
129
  signal MTIME_INC,MTIMEH_INC : std_logic;
130
  signal MSCRATCH_q : SDWORD_T;
131
  signal MEPC_q : SDWORD_T;
132
  signal MCAUSE_q : SDWORD_T;
133
  signal MBADADDR_q : SDWORD_T;
134
  signal MIP_q : SDWORD_T;
135
  signal MBASE_q : SDWORD_T;
136
  signal MBOUND_q : SDWORD_T;
137
  signal MIBASE_q : SDWORD_T;
138
  signal MIBOUND_q : SDWORD_T;
139
  signal MDBASE_q : SDWORD_T;
140
  signal MDBOUND_q : SDWORD_T;
141
  signal MTDELEG_q : SDWORD_T;
142
  signal MTOHOST_q : SDWORD_T;
143
  signal MFROMHOST_q : SDWORD_T;
144
  signal MRV01CC_q : SDWORD_T;
145
  signal CSRQ,CSRQ_q,CSRQ_q2 : SDWORD_T;
146
  signal ILLG,ILLG_q,ILLG_q2 : std_logic;
147
  signal ADR_ERR : std_logic;
148
  signal TMR_MTCH,TMR_INT,TMR_INT_q : std_logic;
149
  signal SFT_INT,SFT_INT_q : std_logic;
150
  signal MMODE : std_logic;
151
  signal MTOHOST_OE,MTOHOST_OE_q : std_logic;
152
  signal UCYCLE,UCYCLE_q,UCYCLE_q2 : natural range 0 to MAX_CNT;
153
  signal UCYCLEH,UCYCLEH_q : natural range 0 to MAX_CNT;
154
  signal UINSTRET,UINSTRET_q,UINSTRET_q2 : natural range 0 to MAX_CNT;
155
  signal UINSTRETH,UINSTRETH_q : natural range 0 to MAX_CNT;
156
  signal USTATS_q : SDWORD_T;
157
  signal UCYCLEH_INC,UINSTRETH_INC : std_logic;
158
  signal UFCSR_q : SDWORD_T;
159
  signal UFRM_q : SDWORD_T;
160
  signal VM : std_logic_vector(4-1 downto 0);
161
  signal IADR,IADR_q : unsigned(12-1 downto 0);
162
  --signal RSTVA_q : ADR_T;
163
  signal UTRPVA_q : ADR_T;
164
  signal MTRPVA_q : ADR_T;
165
  signal IBASE : ADR_T;
166
  signal DBASE : ADR_T;
167
  signal CPQ : SDWORD_T;
168
  signal CP_WE_q : std_logic;
169
  signal CP_IADR,CP_IADR_q : unsigned(12-1 downto 0);
170
  signal CP_D_q : SDWORD_T;
171
 
172
  function push_stack(
173
    S : SDWORD_T;
174
    D : std_logic_vector(3-1 downto 0)
175
  ) return SDWORD_T is
176
    variable TMP : SDWORD_T;
177
  begin
178
    TMP(SDLEN-1 downto 12) := S(SDLEN-1 downto 12);
179
    TMP(12-1 downto 3) := S(9-1 downto 0);
180
    TMP(3-1 downto 0) := to_signed(D);
181
    return(TMP);
182
  end function;
183
 
184
  function pop_stack(
185
    S : SDWORD_T;
186
    D : std_logic_vector(3-1 downto 0)
187
  ) return SDWORD_T is
188
    variable TMP : SDWORD_T;
189
  begin
190
    TMP(SDLEN-1 downto 12) := S(SDLEN-1 downto 12);
191
    TMP(12-1 downto 9) := to_signed(D);
192
    TMP(9-1 downto 0) := S(12-1 downto 3);
193
    return(TMP);
194
  end function;
195
 
196
  function update_csr(
197
    PREVD : SDWORD_T;
198
    MSK : SDWORD_T;
199
    NEWD : SDWORD_T
200
  ) return SDWORD_T is
201
    variable TMP1,TMP2,TMP3 : SDWORD_T;
202
  begin
203
    -- clear new data non-writable bits
204
    TMP1 := NEWD and MSK;
205
    -- clear previous data writable bits
206
    TMP2 := PREVD and not(MSK);
207
    -- build merged data
208
    TMP3 := TMP1 or TMP2;
209
    return(TMP3);
210
  end function;
211
 
212
begin
213
 
214
  ----------------------------------------------
215
 
216
  IADR <= to_unsigned(ADR_i);
217
 
218
  process(CLK_i)
219
  begin
220
    if(CLK_i = '1' and CLK_i'event) then
221
      if(RST_i = '1') then
222
        WE_q <= '0';
223
      else
224
        WE_q <= WE_i;
225
      end if;
226
      CS_OP_q <= CS_OP_i;
227
      IADR_q <= IADR;
228
      CSRD_q <= CSRD_i;
229
      RS1_q <= RS1_i;
230
    end if;
231
  end process;
232
 
233
  IWE <= WE_q and not(HALT_i);
234
 
235
  ----------------------------------------------
236
 
237
  CP_IADR <= to_unsigned(CPADR_i(14-1 downto 2));
238
 
239
  process(CLK_i)
240
  begin
241
    if(CLK_i = '1' and CLK_i'event) then
242
      if(RST_i = '1') then
243
        CP_WE_q <= '0';
244
      else
245
        CP_WE_q <= CPWE_i and CPADR_i(16);
246
      end if;
247
    end if;
248
  end process;
249
 
250
  process(CLK_i)
251
  begin
252
    if(CLK_i = '1' and CLK_i'event) then
253
      CP_IADR_q <= CP_IADR;
254
      CP_D_q <= to_signed(CPD_i);
255
    end if;
256
  end process;
257
 
258
  ----------------------------------------------
259
 
260
  process(CS_OP_q,CSRD_q,RS1_q,CSRQ_q)
261
    variable ZXRS1 : SDWORD_T;
262
  begin
263
 
264
    -- zero-extended RS1
265
    ZXRS1(log2(REGNUM)-1 downto 0) :=
266
      to_signed(RS1_q,log2(REGNUM));
267
    ZXRS1(SDLEN-1 downto log2(REGNUM)) := (others => '0');
268
 
269
    case CS_OP_q is
270
      when CS_RW =>
271
        ICSRD <= CSRD_q;
272
      when CS_RS =>
273
        if(RS1_q = 0) then
274
          ICSRD <= CSRD_q;
275
        else
276
          ICSRD <= CSRQ_q or CSRD_q;
277
        end if;
278
      when CS_RC =>
279
        if(RS1_q = 0) then
280
          ICSRD <= CSRD_q;
281
        else
282
          ICSRD <= CSRQ_q and not(CSRD_q);
283
        end if;
284
      when CS_RWI =>
285
        ICSRD <= ZXRS1;
286
      when CS_RSI =>
287
        if(RS1_q = 0) then
288
          ICSRD <= CSRD_q;
289
        else
290
          ICSRD <= CSRQ_q or ZXRS1;
291
        end if;
292
      when CS_RCI =>
293
        if(RS1_q = 0) then
294
          ICSRD <= CSRD_q;
295
        else
296
          ICSRD <= CSRQ_q and not(ZXRS1);
297
        end if;
298
      when others =>
299
        ICSRD <= CSRD_q;
300
    end case;
301
  end process;
302
 
303
  CS_OP_V <= IX1_V0_i when (
304
    CS_OP_i = CS_RW or
305
    CS_OP_i = CS_RS or
306
    CS_OP_i = CS_RC or
307
    CS_OP_i = CS_RWI or
308
    CS_OP_i = CS_RSI or
309
    CS_OP_i = CS_RCI
310
  ) else '0';
311
 
312
  ----------------------------------------------
313
  -- MSTATUS register
314
  ----------------------------------------------
315
 
316
  -- Note: mode/IE stack is used to handle debug
317
  -- mode too, this however works only if supervisor
318
  -- and hypervisor modes are not implemented (otherwise
319
  -- a mode/IE pair may get deleted).
320
 
321
  process(CLK_i)
322
  begin
323
    if(CLK_i = '1' and CLK_i'event) then
324
      if(RST_i = '1') then
325
        MSTATUS_q <= MSTATUS_RST;
326
      elsif(CP_WE_q = '1' and CP_IADR_q = MSTATUS_ADR and DMODE_i = '1') then
327
        MSTATUS_q <= update_csr(MSTATUS_q,MSTATUS_WMSK,CP_D_q);
328
      elsif(IWE = '1' and IADR_q = MSTATUS_ADR and MMODE = '1') then
329
        MSTATUS_q <= update_csr(MSTATUS_q,MSTATUS_WMSK,ICSRD);
330
      elsif(EXCP_i = '1') then
331
        MSTATUS_q <= push_stack(MSTATUS_q,"110");
332
      elsif(ERET_i = '1') then
333
        MSTATUS_q <= pop_stack(MSTATUS_q,"001");
334
      end if;
335
    end if;
336
  end process;
337
 
338
  -- Machine mode flag
339
  MMODE <= '1' when (
340
    MSTATUS_q(2 downto 1) = "11" or DMODE_i = '1'
341
  ) else '0';
342
 
343
  -- Interrupt Enable flag
344
  IE_o <= '0' when (
345
    (DMODE_i = '1') or
346
    (DIE_i = '0')
347
  ) else MSTATUS_q(0);
348
 
349
  -- Virtual addressing env.
350
  VM <= to_std_logic_vector(MSTATUS_q(21 downto 18));
351
 
352
  MSTATUS_o <= MSTATUS_q;
353
 
354
  ----------------------------------------------
355
  -- MTVEC register
356
  ----------------------------------------------
357
 
358
  -- MTVEC is a R/W register, but only MTVEC_LO and
359
  -- MTVEC_HI patterns can be written to it.
360
 
361
  process(CLK_i)
362
  begin
363
    if(CLK_i = '1' and CLK_i'event) then
364
      if(RST_i = '1') then
365
        MTVEC_q <= to_signed(MTVEC_LO);
366
      elsif(CP_WE_q = '1' and CP_IADR_q = MTVEC_ADR and DMODE_i = '1') then
367
        MTVEC_q <= CP_D_q;
368
      elsif(IWE = '1' and IADR_q = MTVEC_ADR and MMODE = '1') then
369
        if(to_unsigned(ICSRD) = MTVEC_LO) then
370
          MTVEC_q <= to_signed(MTVEC_LO);
371
        elsif(to_unsigned(ICSRD) = MTVEC_HI) then
372
          MTVEC_q <= to_signed(MTVEC_HI);
373
        end if;
374
      end if;
375
    end if;
376
  end process;
377
 
378
  -- Set reset, user mode trap and machine mode trap vectors
379
  -- according to mtvec value (at reset, trap vectors location
380
  -- is set to "low").
381
 
382
  process(CLK_i)
383
  begin
384
    if(CLK_i = '1' and CLK_i'event) then
385
      if(RST_i = '1') then
386
        UTRPVA_q <= UTRAP_VA_LO;
387
        MTRPVA_q <= MTRAP_VA_LO;
388
      elsif(CP_WE_q = '1' and CP_IADR_q = MTVEC_ADR and DMODE_i = '1') then
389
        if(to_unsigned(CP_D_q) = MTVEC_LO) then
390
          UTRPVA_q <= UTRAP_VA_LO;
391
          MTRPVA_q <= MTRAP_VA_LO;
392
        elsif(to_unsigned(CP_D_q) = MTVEC_HI) then
393
          UTRPVA_q <= UTRAP_VA_HI;
394
          MTRPVA_q <= MTRAP_VA_HI;
395
        end if;
396
      elsif(IWE = '1' and IADR_q = MTVEC_ADR and MMODE = '1') then
397
        if(to_unsigned(ICSRD) = MTVEC_LO) then
398
          UTRPVA_q <= UTRAP_VA_LO;
399
          MTRPVA_q <= MTRAP_VA_LO;
400
        elsif(to_unsigned(ICSRD) = MTVEC_HI) then
401
          UTRPVA_q <= UTRAP_VA_HI;
402
          MTRPVA_q <= MTRAP_VA_HI;
403
        end if;
404
      end if;
405
    end if;
406
  end process;
407
 
408
  -- exception trap vector address
409
  ETVA_o <=
410
    MTRPVA_q when MMODE = '1' else
411
    UTRPVA_q;
412
 
413
  ----------------------------------------------
414
  -- MTDELEG register
415
  ----------------------------------------------
416
 
417
  -- This register is not implemented.
418
 
419
  MTDELEG_q <= (others => '0');
420
 
421
  ----------------------------------------------
422
  -- MIE register
423
  ----------------------------------------------
424
 
425
  process(CLK_i)
426
  begin
427
    if(CLK_i = '1' and CLK_i'event) then
428
      if(RST_i = '1') then
429
        MIE_q <= (others => '0');
430
      elsif(CP_WE_q = '1' and CP_IADR_q = MIE_ADR and DMODE_i = '1') then
431
        MIE_q <= update_csr(MIE_q,MIE_WMSK,CP_D_q);
432
      elsif(IWE = '1' and IADR_q = MIE_ADR and MMODE = '1') then
433
        MIE_q <= update_csr(MIE_q,MIE_WMSK,ICSRD);
434
      end if;
435
    end if;
436
  end process;
437
 
438
  ----------------------------------------------
439
  -- MTIMECMP register
440
  ----------------------------------------------
441
 
442
  process(CLK_i)
443
  begin
444
    if(CLK_i = '1' and CLK_i'event) then
445
      if(RST_i = '1') then
446
        MTIMECMP_q <= 0;
447
      elsif(CP_WE_q = '1' and CP_IADR_q = MTIMECMP_ADR and DMODE_i = '1') then
448
        MTIMECMP_q <= to_integer(CP_D_q);
449
      elsif(IWE = '1' and IADR_q = MTIMECMP_ADR and MMODE = '1') then
450
        MTIMECMP_q <= to_integer(ICSRD);
451
      end if;
452
    end if;
453
  end process;
454
 
455
  -- TMR_INT_q remains asserted after an TMR_INT pulse
456
  -- until this event can be recorded in MIP register
457
  -- (this precaution is needed to insure that TMR_INT
458
  -- pulse is not lost because of a write to MIP).
459
 
460
  TMR_INT <= '1' when (MTIMECMP_q = MTIME_q) else '0';
461
 
462
  --process(CLK_i)
463
  --begin
464
  --  if(CLK_i = '1' and CLK_i'event) then
465
  --    if(RST_i = '1') then
466
  --      TMR_INT_q <= '0';
467
  --    elsif(TMR_INT_q = '0' and TMR_INT = '1') then
468
  --      TMR_INT_q <= '1';
469
  --    elsif(MIP_q(MTIE) = '1') then
470
  --      TMR_INT_q <= '0';
471
  --    end if;
472
  --  end if;
473
  --end process;
474
 
475
  ----------------------------------------------
476
  -- MTIME register 
477
  -- (Machine Mode Cycles counter)
478
  ----------------------------------------------
479
 
480
  -- mcycle register is incremented at every cycle,
481
  -- and wraps-up when count reaches MAX_CNT value.
482
 
483
  process(CLK_i)
484
  begin
485
    if(CLK_i = '1' and CLK_i'event) then
486
      if(RST_i = '1') then
487
        MTIME_q <= 0;
488
      elsif(CP_WE_q = '1' and CP_IADR_q = MTIME_ADR and DMODE_i = '1') then
489
        MTIME_q <= to_integer(CP_D_q);
490
      elsif(IWE = '1' and IADR_q = MTIME_ADR and MMODE = '1') then
491
        MTIME_q <= to_integer(ICSRD);
492
      else
493
        MTIME_q <= MTIME;
494
      end if;
495
      MTIME_q2 <= MTIME_q;
496
    end if;
497
  end process;
498
 
499
  process(MTIME_q)
500
  begin
501
    if(MTIME_q < MAX_CNT) then
502
      MTIME <= MTIME_q + 1;
503
      MTIMEH_INC <= '0';
504
    else
505
      MTIME <= 0;
506
      MTIMEH_INC <= '1';
507
    end if;
508
  end process;
509
 
510
  -- ucycleh register is incremented when ucycle
511
  -- one wraps-up. ucycleh wraps-up when count
512
  -- reaches MAX_CNT value.
513
 
514
  process(CLK_i)
515
  begin
516
    if(CLK_i = '1' and CLK_i'event) then
517
      if(RST_i = '1') then
518
        MTIMEH_q <= 0;
519
      elsif(CP_WE_q = '1' and CP_IADR_q = MTIMEH_ADR and DMODE_i = '1') then
520
        MTIMEH_q <= to_integer(CP_D_q);
521
      elsif(IWE = '1' and IADR_q = MTIMEH_ADR and MMODE = '1') then
522
        MTIMEH_q <= to_integer(ICSRD);
523
      elsif(MTIMEH_INC = '1') then
524
        MTIMEH_q <= MTIMEH;
525
      end if;
526
    end if;
527
  end process;
528
 
529
  process(MTIMEH_q)
530
  begin
531
    if(MTIMEH_q < MAX_CNT) then
532
      MTIMEH <= MTIMEH_q + 1;
533
    else
534
      MTIMEH <= 0;
535
    end if;
536
  end process;
537
 
538
  ----------------------------------------------
539
  -- MSCRATCH register
540
  ----------------------------------------------
541
 
542
  process(CLK_i)
543
  begin
544
    if(CLK_i = '1' and CLK_i'event) then
545
      if(RST_i = '1') then
546
        MSCRATCH_q <= (others => '0');
547
      elsif(CP_WE_q = '1' and CP_IADR_q = MSCRATCH_ADR and DMODE_i = '1') then
548
        MSCRATCH_q <= CP_D_q;
549
      elsif(IWE = '1' and IADR_q = MSCRATCH_ADR and MMODE = '1') then
550
        MSCRATCH_q <= ICSRD; --CSRD_q;
551
      end if;
552
    end if;
553
  end process;
554
 
555
  ----------------------------------------------
556
  -- MEPC register
557
  ----------------------------------------------
558
 
559
  process(CLK_i)
560
  begin
561
    if(CLK_i = '1' and CLK_i'event) then
562
      if(RST_i = '1') then
563
        MEPC_q <= (others => '0');
564
      elsif(CP_WE_q = '1' and CP_IADR_q = MEPC_ADR and DMODE_i = '1') then
565
        MEPC_q <= update_csr(MEPC_q,MEPC_WMSK,CP_D_q);
566
      elsif(IWE = '1' and IADR_q = MEPC_ADR and MMODE = '1') then
567
        MEPC_q <= update_csr(MEPC_q,MEPC_WMSK,ICSRD);
568
      elsif(EXCP_i = '1') then
569
        MEPC_q <= to_signed(EPC_i);
570
      end if;
571
    end if;
572
  end process;
573
 
574
  -- mepc content must be made available to fetch
575
  -- logic when returning from an exception.
576
 
577
  MEPC_o <= to_unsigned(MEPC_q);
578
 
579
  ----------------------------------------------
580
  -- MCAUSE register
581
  ----------------------------------------------
582
 
583
  process(CLK_i)
584
  begin
585
    if(CLK_i = '1' and CLK_i'event) then
586
      if(RST_i = '1') then
587
        MCAUSE_q <= (others => '0');
588
      elsif(CP_WE_q = '1' and CP_IADR_q = MCAUSE_ADR and DMODE_i = '1') then
589
        MCAUSE_q <= update_csr(MCAUSE_q,MCAUSE_WMSK,CP_D_q);
590
      elsif(IWE = '1' and IADR_q = MCAUSE_ADR and MMODE = '1') then
591
        MCAUSE_q <= update_csr(MCAUSE_q,MCAUSE_WMSK,ICSRD);
592
      elsif(EXCP_i = '1') then
593
        MCAUSE_q(SDLEN-2 downto 4) <= (others => '0');
594
        if(ECAUSE_i = UMCALL and MMODE = '1') then
595
          MCAUSE_q(4-1 downto 0) <= to_signed(MMCALL(4-1 downto 0));
596
        else
597
          MCAUSE_q(4-1 downto 0) <= to_signed(ECAUSE_i(4-1 downto 0));
598
        end if;
599
        MCAUSE_q(SDLEN-1) <= ECAUSE_i(4);
600
      end if;
601
    end if;
602
  end process;
603
 
604
  ----------------------------------------------
605
  -- MBADADDR register
606
  ----------------------------------------------
607
 
608
  process(CLK_i)
609
  begin
610
    if(CLK_i = '1' and CLK_i'event) then
611
      if(RST_i = '1') then
612
        MBADADDR_q <= (others => '0');
613
      elsif(CP_WE_q = '1' and CP_IADR_q = MBADADDR_ADR and DMODE_i = '1') then
614
        MBADADDR_q <= CP_D_q;
615
      elsif(IWE = '1' and IADR_q = MBADADDR_ADR and MMODE = '1') then
616
        MBADADDR_q <= ICSRD;
617
      elsif(EXCP_i = '1') then
618
        MBADADDR_q <= to_signed(EBADR_i);
619
      end if;
620
    end if;
621
  end process;
622
 
623
  ----------------------------------------------
624
  -- MIP register
625
  ----------------------------------------------
626
 
627
  -- Note: while timer interrupt pending bit is implicilty
628
  -- cleared by writing mtimecmp register, software interrupt
629
  -- pending bit must be explicitly cleared by writing mip
630
  -- register.
631
 
632
  process(CLK_i)
633
  begin
634
    if(CLK_i = '1' and CLK_i'event) then
635
      if(RST_i = '1') then
636
        MIP_q <= (others => '0');
637
      elsif(CP_WE_q = '1' and CP_IADR_q = MIP_ADR and DMODE_i = '1') then
638
        MIP_q <= update_csr(MIP_q,MIP_WMSK,CP_D_q);
639
      elsif(IWE = '1' and IADR_q = MIP_ADR and MMODE = '1') then
640
        -- mip is written as a CSR (even if only MSIP bit is writable)
641
        MIP_q <= update_csr(MIP_q,MIP_WMSK,ICSRD);
642
      elsif(TMR_INT = '1') then
643
        MIP_q(MTIP) <= '1';
644
      elsif(
645
        (IWE = '1' and IADR_q = MTIMECMP_ADR and MMODE = '1') or
646
        (CP_WE_q = '1' and CP_IADR_q = MTIMECMP_ADR and DMODE_i = '1')
647
      ) then
648
        -- mip[MTIP] is cleared when mtimecmp register is written
649
        MIP_q(MTIP) <= '0';
650
      end if;
651
    end if;
652
  end process;
653
 
654
  -- Timer Interrupt exception is actually raised only if:
655
  -- 1) mip[MTIP] is set, AND
656
  -- 2) timer int's are enabled by mie[MTIE], AND
657
  -- 3) exceptions are anabled by mstatus[IE].
658
 
659
  TMR_INT_o <= MIP_q(MTIP) and MIE_q(MTIE) and MSTATUS_q(0);
660
 
661
  --process(CLK_i)
662
  --begin
663
  --  if(CLK_i = '1' and CLK_i'event) then
664
  --    if(RST_i = '1') then
665
  --      TMR_INT_q <= '0';
666
  --    else
667
  --      TMR_INT_q <= TMR_INT;
668
  --    end if;
669
  --  end if;
670
  --end process;
671
 
672
  --TMR_INT_o <= TMR_INT_q;
673
 
674
  -- Soft Interrupt exception is actually raised only if:
675
  -- 1) mip[MSIP] is set, AND
676
  -- 2) soft int's are enabled by mie[MSIE], AND
677
  -- 3) exceptions are anabled by mstatus[IE].
678
 
679
  SFT_INT <= MIP_q(MSIP) and MIE_q(MSIE) and MSTATUS_q(0);
680
 
681
  process(CLK_i)
682
  begin
683
    if(CLK_i = '1' and CLK_i'event) then
684
      if(RST_i = '1') then
685
        SFT_INT_q <= '0';
686
      else
687
        SFT_INT_q <= SFT_INT;
688
      end if;
689
    end if;
690
  end process;
691
 
692
  SFT_INT_o <= SFT_INT_q;
693
 
694
  ----------------------------------------------
695
  -- MBASE register
696
  ----------------------------------------------
697
 
698
  process(CLK_i)
699
  begin
700
    if(CLK_i = '1' and CLK_i'event) then
701
      if(RST_i = '1') then
702
        MBASE_q <= (others => '0');
703
      elsif(CP_WE_q = '1' and CP_IADR_q = MBASE_ADR and DMODE_i = '1') then
704
        MBASE_q <= CP_D_q;
705
      elsif(IWE = '1' and IADR_q = MBASE_ADR and MMODE = '1') then
706
        MBASE_q <= ICSRD;
707
      end if;
708
    end if;
709
  end process;
710
 
711
  MBASE_o <= to_unsigned(MBASE_q);
712
 
713
  ----------------------------------------------
714
  -- MBOUND register
715
  ----------------------------------------------
716
 
717
  process(CLK_i)
718
  begin
719
    if(CLK_i = '1' and CLK_i'event) then
720
      if(RST_i = '1') then
721
        MBOUND_q <= (others => '0');
722
      elsif(CP_WE_q = '1' and CP_IADR_q = MBOUND_ADR and DMODE_i = '1') then
723
        MBOUND_q <= CP_D_q;
724
      elsif(IWE = '1' and IADR_q = MBOUND_ADR and MMODE = '1') then
725
        MBOUND_q <= ICSRD;
726
      end if;
727
    end if;
728
  end process;
729
 
730
  MBOUND_o <= to_unsigned(MBOUND_q);
731
 
732
  ----------------------------------------------
733
  -- MIBASE register
734
  ----------------------------------------------
735
 
736
  process(CLK_i)
737
  begin
738
    if(CLK_i = '1' and CLK_i'event) then
739
      if(RST_i = '1') then
740
        MIBASE_q <= (others => '0');
741
      elsif(CP_WE_q = '1' and CP_IADR_q = MIBASE_ADR and DMODE_i = '1') then
742
        MIBASE_q <= CP_D_q;
743
      elsif(IWE = '1' and IADR_q = MIBASE_ADR and MMODE = '1') then
744
        MIBASE_q <= ICSRD;
745
      elsif(
746
        IWE = '1' and IADR_q = MBASE_ADR and
747
        MMODE = '1' and VM = MBBID
748
      ) then
749
        MIBASE_q <= ICSRD;
750
      end if;
751
    end if;
752
  end process;
753
 
754
  MIBASE_o <= to_unsigned(MIBASE_q);
755
 
756
  ----------------------------------------------
757
  -- MIBOUND register
758
  ----------------------------------------------
759
 
760
  process(CLK_i)
761
  begin
762
    if(CLK_i = '1' and CLK_i'event) then
763
      if(RST_i = '1') then
764
        MIBOUND_q <= (others => '0');
765
      elsif(CP_WE_q = '1' and CP_IADR_q = MIBOUND_ADR and DMODE_i = '1') then
766
        MIBOUND_q <= CP_D_q;
767
      elsif(IWE = '1' and IADR_q = MIBOUND_ADR and MMODE = '1') then
768
        MIBOUND_q <= ICSRD;
769
      elsif(
770
        IWE = '1' and IADR_q = MBOUND_ADR and
771
        MMODE = '1' and VM = MBBID
772
      ) then
773
        MIBOUND_q <= ICSRD;
774
      end if;
775
    end if;
776
  end process;
777
 
778
  MIBOUND_o <= to_unsigned(MIBOUND_q);
779
 
780
  ----------------------------------------------
781
  -- MDBASE register
782
  ----------------------------------------------
783
 
784
  process(CLK_i)
785
  begin
786
    if(CLK_i = '1' and CLK_i'event) then
787
      if(RST_i = '1') then
788
        MDBASE_q <= (others => '0');
789
      elsif(CP_WE_q = '1' and CP_IADR_q = MDBASE_ADR and DMODE_i = '1') then
790
        MDBASE_q <= CP_D_q;
791
      elsif(IWE = '1' and IADR_q = MDBASE_ADR and MMODE = '1') then
792
        MDBASE_q <= ICSRD;
793
      elsif(
794
        IWE = '1' and IADR_q = MBASE_ADR and
795
        MMODE = '1' and VM = MBBID
796
      ) then
797
        MDBASE_q <= ICSRD;
798
      end if;
799
    end if;
800
  end process;
801
 
802
  MDBASE_o <= to_unsigned(MDBASE_q);
803
 
804
  ----------------------------------------------
805
  -- MDBOUND register
806
  ----------------------------------------------
807
 
808
  process(CLK_i)
809
  begin
810
    if(CLK_i = '1' and CLK_i'event) then
811
      if(RST_i = '1') then
812
        MDBOUND_q <= (others => '0');
813
      elsif(CP_WE_q = '1' and CP_IADR_q = MDBOUND_ADR and DMODE_i = '1') then
814
        MDBOUND_q <= CP_D_q;
815
      elsif(IWE = '1' and IADR_q = MDBOUND_ADR and MMODE = '1') then
816
        MDBOUND_q <= ICSRD;
817
      elsif(
818
        IWE = '1' and IADR_q = MBOUND_ADR and
819
        MMODE = '1' and VM = MBBID
820
      ) then
821
        MDBOUND_q <= ICSRD;
822
      end if;
823
    end if;
824
  end process;
825
 
826
  MDBOUND_o <= to_unsigned(MDBOUND_q);
827
 
828
  ----------------------------------------------
829
  -- MTOHOST register
830
  ----------------------------------------------
831
 
832
  process(CLK_i)
833
  begin
834
    if(CLK_i = '1' and CLK_i'event) then
835
      if(RST_i = '1') then
836
        MTOHOST_q <= (others => '0');
837
      elsif(CP_WE_q = '1' and CP_IADR_q = MTOHOST_ADR and DMODE_i = '1') then
838
        MTOHOST_q <= CP_D_q;
839
      elsif(IWE = '1' and IADR_q = MTOHOST_ADR and MMODE = '1') then
840
        MTOHOST_q <= ICSRD;
841
      end if;
842
    end if;
843
  end process;
844
 
845
  MTOHOST_OE <= IWE when(
846
    IADR_q = MTOHOST_ADR and MMODE = '1'
847
  ) else '0';
848
 
849
  process(CLK_i)
850
  begin
851
    if(CLK_i = '1' and CLK_i'event) then
852
      if(RST_i = '1') then
853
        MTOHOST_OE_q <= '0';
854
      else
855
        MTOHOST_OE_q <= MTOHOST_OE ;
856
      end if;
857
    end if;
858
  end process;
859
 
860
  MTOHOST_OE_o <= MTOHOST_OE_q;
861
 
862
  MTOHOST_o <= to_std_logic_vector(MTOHOST_q);
863
 
864
  ----------------------------------------------
865
  -- MFROMHOST register
866
  ----------------------------------------------
867
 
868
  process(CLK_i)
869
  begin
870
    if(CLK_i = '1' and CLK_i'event) then
871
      if(RST_i = '1') then
872
        MFROMHOST_q <= (others => '0');
873
      elsif(CP_WE_q = '1' and CP_IADR_q = MFROMHOST_ADR and DMODE_i = '1') then
874
        MFROMHOST_q <= CP_D_q;
875
      elsif(MFROMHOST_WE_i = '1') then
876
        MFROMHOST_q <= to_signed(MFROMHOST_i);
877
      elsif(IWE = '1' and IADR_q = MFROMHOST_ADR and MMODE = '1') then
878
        MFROMHOST_q <= ICSRD;
879
      end if;
880
    end if;
881
  end process;
882
 
883
  ----------------------------------------------
884
  -- RV01 Core Control register (MRV01CC)
885
  ----------------------------------------------
886
 
887
  -- This register allows to enable/disable 
888
  -- implementation dependent features. In the
889
  -- this version of RV01 core, the only feature
890
  -- that can be enabled/disabled through MRV01CC
891
  -- register is parallel execution capability.
892
 
893
  GPXE_1 : if(PXE = '1') generate
894
 
895
  process(CLK_i)
896
  begin
897
    if(CLK_i = '1' and CLK_i'event) then
898
      if(RST_i = '1') then
899
        MRV01CC_q <= MRV01CC_RST;
900
      elsif(CP_WE_q = '1' and CP_IADR_q = MRV01CC_ADR and DMODE_i = '1') then
901
        MRV01CC_q <= update_csr(MRV01CC_q,MRV01CC_WMSK,CP_D_q);
902
      elsif(IWE = '1' and IADR_q = MRV01CC_ADR and MMODE = '1') then
903
        MRV01CC_q <= update_csr(MRV01CC_q,MRV01CC_WMSK,ICSRD);
904
      end if;
905
    end if;
906
  end process;
907
 
908
  end generate;
909
 
910
  GPXE_0 : if(PXE = '0') generate
911
 
912
  MRV01CC_q <= (others => '0');
913
 
914
  end generate;
915
 
916
  PXE_o <= MRV01CC_q(0);
917
 
918
  ----------------------------------------------
919
  -- UTIME/H registers
920
  -- (User Mode Cycles counter)
921
  ----------------------------------------------
922
 
923
  -- ucycle register is incremented at every cycle,
924
  -- and wraps-up when count reaches MAX_CNT value.
925
 
926
  process(CLK_i)
927
  begin
928
    if(CLK_i = '1' and CLK_i'event) then
929
      if(RST_i = '1') then
930
        UCYCLE_q <= 0;
931
      else
932
        UCYCLE_q <= UCYCLE;
933
      end if;
934
      UCYCLE_q2 <= UCYCLE_q;
935
    end if;
936
  end process;
937
 
938
  process(UCYCLE_q,STOPCYCLE_i,STOPTIME_i)
939
  begin
940
    if(UCYCLE_q < MAX_CNT and STOPCYCLE_i = '0' and STOPTIME_i = '0') then
941
      UCYCLE <= UCYCLE_q + 1;
942
      UCYCLEH_INC <= '0';
943
    else
944
      UCYCLE <= 0;
945
      UCYCLEH_INC <= '1';
946
    end if;
947
  end process;
948
 
949
  -- ucycleh register is incremented when ucycle
950
  -- one wraps-up. ucycleh wraps-up when count
951
  -- reaches MAX_CNT value.
952
 
953
  process(CLK_i)
954
  begin
955
    if(CLK_i = '1' and CLK_i'event) then
956
      if(RST_i = '1') then
957
        UCYCLEH_q <= 0;
958
      elsif(UCYCLEH_INC = '1' and STOPCYCLE_i = '0' and STOPTIME_i = '0') then
959
        UCYCLEH_q <= UCYCLEH;
960
      end if;
961
    end if;
962
  end process;
963
 
964
  process(UCYCLEH_q)
965
  begin
966
    if(UCYCLEH_q < MAX_CNT) then
967
      UCYCLEH <= UCYCLEH_q + 1;
968
    else
969
      UCYCLEH <= 0;
970
    end if;
971
  end process;
972
 
973
  ----------------------------------------------
974
  -- Retired instructions counter
975
  ----------------------------------------------
976
 
977
  -- uinstret register is incremented every time
978
  -- an instruction is retired (this event may occur
979
  -- twice per cycle), and wraps-up when count reaches
980
  -- MAX_CNT value.
981
  -- Instruction eret is a special case because it
982
  -- must be counted as a valid instruction even if
983
  -- its valid bit is cleared.
984
 
985
  process(CLK_i)
986
  begin
987
    if(CLK_i = '1' and CLK_i'event) then
988
      if(RST_i = '1') then
989
        UINSTRET_q <= 0;
990
      else
991
        UINSTRET_q <= UINSTRET;
992
      end if;
993
      UINSTRET_q2 <= UINSTRET_q;
994
    end if;
995
  end process;
996
 
997
  process(UINSTRET_q,IX3_V_i,ERET_i,STOPCYCLE_i)
998
  begin
999
    if(IX3_V_i = "11" and STOPCYCLE_i = '0') then
1000
      if(UINSTRET_q < MAX_CNT-1) then
1001
        UINSTRET <= UINSTRET_q + 2;
1002
        UINSTRETH_INC <= '0';
1003
      elsif(UINSTRET_q = MAX_CNT-1) then
1004
        UINSTRET <= 0;
1005
        UINSTRETH_INC <= '1';
1006
      else
1007
        UINSTRET <= 1;
1008
        UINSTRETH_INC <= '1';
1009
      end if;
1010
    elsif((IX3_V_i(0) = '1' or IX3_V_i(1) = '1') and STOPCYCLE_i = '0') then
1011
      if(UINSTRET_q < MAX_CNT) then
1012
        UINSTRET <= UINSTRET_q + 1;
1013
        UINSTRETH_INC <= '0';
1014
      else
1015
        UINSTRET <= 0;
1016
        UINSTRETH_INC <= '1';
1017
      end if;
1018
    else
1019
      UINSTRET <= UINSTRET_q;
1020
      UINSTRETH_INC <= '0';
1021
    end if;
1022
  end process;
1023
 
1024
  -- uinstreth register is incremented when uinstret
1025
  -- one wraps-up. uinstreth wraps-up when count
1026
  -- reaches MAX_CNT value.
1027
 
1028
  process(CLK_i)
1029
  begin
1030
    if(CLK_i = '1' and CLK_i'event) then
1031
      if(RST_i = '1') then
1032
        UINSTRETH_q <= 0;
1033
      elsif(UINSTRETH_INC = '1' and STOPCYCLE_i = '0') then
1034
        UINSTRETH_q <= UINSTRETH;
1035
      end if;
1036
    end if;
1037
  end process;
1038
 
1039
  process(UINSTRETH_q)
1040
  begin
1041
    if(UINSTRETH_q < MAX_CNT) then
1042
      UINSTRETH <= UINSTRETH_q + 1;
1043
    else
1044
      UINSTRETH <= 0;
1045
    end if;
1046
  end process;
1047
 
1048
  ----------------------------------------------
1049
  -- UFCSR, UFFLAGS and UFRM registers
1050
  ----------------------------------------------
1051
 
1052
  GF0_1 : if(FPU_PRESENT = '1') generate
1053
 
1054
  -- UFFLAGS register is implicitly implemented by UFCSR[4:0],
1055
  -- while UFRM is explicitly implemented.
1056
  -- A write to UFCSR modifies UFFLAGS[4:0] (implicitly) and
1057
  -- UFRM[2:0] (explicitly).
1058
  -- A write to UFFLAGS modifies UFCSR bits [4:0] only.
1059
  -- A write to UFRM modifies UFRM and UFCSR bits [7:5].
1060
 
1061
  process(CLK_i)
1062
  variable TMP : SDWORD_T;
1063
  begin
1064
    if(CLK_i = '1' and CLK_i'event) then
1065
      if(RST_i = '1') then
1066
        UFCSR_q <= (others => '0');
1067
      elsif(IWE = '1' and IADR_q = UFCSR_ADR) then
1068
        UFCSR_q <= update_csr(UFCSR_q,UFCSR_WMSK,ICSRD);
1069
      elsif(IWE = '1' and IADR_q = UFFLAGS_ADR) then
1070
        UFCSR_q <= update_csr(UFCSR_q,UFFLAGS_WMSK,ICSRD);
1071
      elsif(IWE = '1' and IADR_q = UFRM_ADR) then
1072
        TMP := update_csr(UFRM_q,UFRM_WMSK,ICSRD);
1073
        UFCSR_q <= UFCSR_q(SDLEN-1 downto 8) & TMP(3-1 downto 0) & UFCSR_q(5-1 downto 0) ;
1074
      end if;
1075
    end if;
1076
  end process;
1077
 
1078
  process(CLK_i)
1079
  variable TMP : SDWORD_T;
1080
  begin
1081
    if(CLK_i = '1' and CLK_i'event) then
1082
      if(RST_i = '1') then
1083
        UFRM_q <= (others => '0');
1084
      elsif(IWE = '1' and IADR_q = UFCSR_ADR) then
1085
        TMP := update_csr(UFCSR_q,UFCSR_WMSK,ICSRD);
1086
        UFRM_q <= UFRM_q(SDLEN-1 downto 3) & TMP(3-1 downto 0);
1087
      elsif(IWE = '1' and IADR_q = UFRM_ADR) then
1088
        UFRM_q <= update_csr(UFRM_q,UFRM_WMSK,ICSRD);
1089
      end if;
1090
    end if;
1091
  end process;
1092
 
1093
  FFLAGS_o <= to_std_logic_vector(UFCSR_q(5-1 downto 0));
1094
  FRM_o <= to_std_logic_vector(UFCSR_q(8-1 downto 5));
1095
 
1096
  end generate;
1097
 
1098
  GF0_0 : if(FPU_PRESENT = '0') generate
1099
 
1100
  UFCSR_q <= (others => '0');
1101
  UFRM_q <= (others => '0');
1102
  FFLAGS_o <= (others => '0');
1103
  FRM_o <= (others => '0');
1104
 
1105
  end generate;
1106
 
1107
  ----------------------------------------------
1108
  -- Sodor "Stats" register
1109
  ----------------------------------------------
1110
 
1111
  -- This register has been added only for partial
1112
  -- compatibility with Sodor RISC-V core.
1113
 
1114
  process(CLK_i)
1115
  begin
1116
    if(CLK_i = '1' and CLK_i'event) then
1117
      if(IWE = '1' and IADR_q = USTATS_ADR) then
1118
        USTATS_q <= ICSRD;
1119
      end if;
1120
    end if;
1121
  end process;
1122
 
1123
  ----------------------------------------------
1124
  -- CSR output mux
1125
  ----------------------------------------------
1126
 
1127
  -- Notes:
1128
  -- 1) utime(h) register is coincident with ucycle(h) register.
1129
  -- 2) ucycleh and uinstreth are updated with one cycle delay
1130
  -- with respect to ucycle and uinstret, therefore, in order to
1131
  -- get consistent read values, one-cycle delayed version of
1132
  -- ucycle and uinstret are accessed on reads.
1133
  -- 3) mtime register is implemented as mcycle for a matter of
1134
  -- uniformity with utime one.
1135
  -- 4) one-cycle delayed version of mcycle is accessed on reads
1136
  -- (see note #2).
1137
 
1138
  process(IADR,MSTATUS_q,MTVEC_q,MIE_q,MTIMECMP_q,MTIME_q2,MTIMEH_q,
1139
    MSCRATCH_q,MEPC_q,MCAUSE_q,MBADADDR_q,MIP_q,MBASE_q,MBOUND_q,
1140
    MIBASE_q,MIBOUND_q,MDBASE_q,MDBOUND_q,MTOHOST_q,MFROMHOST_q,
1141
    UCYCLE_q2,UINSTRET_q2,UCYCLEH_q,UINSTRETH_q,UFCSR_q,UFRM_q,USTATS_q,
1142
    MTDELEG_q,MRV01CC_q,VM,WE_i,MMODE
1143
  )
1144
  begin
1145
    ADR_ERR <= '0';
1146
    case IADR is
1147
 
1148
      -- User mode CSR's
1149
 
1150
      when UCYCLE_ADR =>
1151
        CSRQ <= to_signed(UCYCLE_q2,SDLEN);
1152
        ADR_ERR <= WE_i;
1153
      when UTIME_ADR =>
1154
        CSRQ <= to_signed(UCYCLE_q2,SDLEN);
1155
        ADR_ERR <= WE_i;
1156
      when UINSTRET_ADR =>
1157
        CSRQ <= to_signed(UINSTRET_q2,SDLEN);
1158
        ADR_ERR <= WE_i;
1159
      when UCYCLEH_ADR =>
1160
        CSRQ <= to_signed(UCYCLEH_q,SDLEN);
1161
        ADR_ERR <= WE_i;
1162
      when UTIMEH_ADR =>
1163
        CSRQ <= to_signed(UCYCLEH_q,SDLEN);
1164
        ADR_ERR <= WE_i;
1165
      when UINSTRETH_ADR =>
1166
        CSRQ <= to_signed(UINSTRETH_q,SDLEN);
1167
        ADR_ERR <= WE_i;
1168
      when UFCSR_ADR =>
1169
        CSRQ <= UFCSR_q;
1170
      when UFFLAGS_ADR =>
1171
        CSRQ(5-1 downto 0) <= UFCSR_q(5-1 downto 0);
1172
        CSRQ(SDLEN-1 downto 5) <= (others => '0');
1173
      when UFRM_ADR =>
1174
        CSRQ <= UFRM_q;
1175
      when USTATS_ADR =>
1176
        CSRQ <= USTATS_q;
1177
 
1178
      -- Machine mode CSR's
1179
 
1180
      when MCPUID_ADR =>
1181
        CSRQ <= MCPUID_RO;
1182
        ADR_ERR <= WE_i or not(MMODE);
1183
      when MIMPID_ADR =>
1184
        CSRQ <= MIMPID_RO;
1185
        ADR_ERR <= WE_i or not(MMODE);
1186
      when MHARTID_ADR =>
1187
        CSRQ <= MHARTID_RO;
1188
        ADR_ERR <= WE_i or not(MMODE);
1189
      when MSTATUS_ADR =>
1190
        CSRQ <= MSTATUS_q;
1191
        ADR_ERR <= not(MMODE);
1192
      when MTVEC_ADR =>
1193
        CSRQ <= MTVEC_q;
1194
        ADR_ERR <= not(MMODE);
1195
      when MTDELEG_ADR =>
1196
        CSRQ <= MTDELEG_q;
1197
        ADR_ERR <= WE_i or not(MMODE);
1198
      when MIE_ADR =>
1199
        CSRQ <= MIE_q;
1200
        ADR_ERR <= not(MMODE);
1201
      when MTIMECMP_ADR =>
1202
        CSRQ <= to_signed(MTIMECMP_q,SDLEN);
1203
        ADR_ERR <= not(MMODE);
1204
      when MTIME_ADR =>
1205
        CSRQ <= to_signed(MTIME_q2,SDLEN);
1206
        ADR_ERR <= not(MMODE);
1207
      when MTIMEH_ADR =>
1208
        CSRQ <= to_signed(MTIMEH_q,SDLEN);
1209
        ADR_ERR <= not(MMODE);
1210
      when MSCRATCH_ADR =>
1211
        CSRQ <= MSCRATCH_q;
1212
        ADR_ERR <= not(MMODE);
1213
      when MEPC_ADR =>
1214
        CSRQ <= MEPC_q;
1215
        ADR_ERR <= not(MMODE);
1216
      when MCAUSE_ADR =>
1217
        CSRQ <= MCAUSE_q;
1218
        ADR_ERR <= not(MMODE);
1219
      when MBADADDR_ADR =>
1220
        CSRQ <= MBADADDR_q;
1221
        ADR_ERR <= not(MMODE);
1222
      when MIP_ADR =>
1223
        CSRQ <= MIP_q;
1224
      when MBASE_ADR =>
1225
        if(VM = MBBID) then -- is it correct?
1226
          CSRQ <= MDBASE_q;
1227
        else
1228
          CSRQ <= MBASE_q;
1229
        end if;
1230
        ADR_ERR <= not(MMODE);
1231
      when MBOUND_ADR =>
1232
        if(VM = MBBID) then -- is it correct?
1233
          CSRQ <= MDBOUND_q;
1234
        else
1235
          CSRQ <= MBOUND_q;
1236
        end if;
1237
        ADR_ERR <= not(MMODE);
1238
      when MIBASE_ADR =>
1239
        CSRQ <= MIBASE_q;
1240
        ADR_ERR <= not(MMODE);
1241
      when MIBOUND_ADR =>
1242
        CSRQ <= MIBOUND_q;
1243
        ADR_ERR <= not(MMODE);
1244
      when MDBASE_ADR =>
1245
        CSRQ <= MDBASE_q;
1246
        ADR_ERR <= not(MMODE);
1247
      when MDBOUND_ADR =>
1248
        CSRQ <= MDBOUND_q;
1249
        ADR_ERR <= not(MMODE);
1250
      --when HTIMEW_ADR =>
1251
      --  CSRQ <= HTIMEW_q;
1252
      --when HTIMEHW_ADR =>
1253
      --  CSRQ <= HTIMEHW_q;
1254
      when MRV01CC_ADR =>
1255
        CSRQ <= MRV01CC_q;
1256
        ADR_ERR <= not(MMODE);
1257
 
1258
      -- Host interface CSR's
1259
 
1260
      when MTOHOST_ADR =>
1261
        CSRQ <= MTOHOST_q;
1262
        ADR_ERR <= not(MMODE);
1263
      when MFROMHOST_ADR =>
1264
        CSRQ <= MFROMHOST_q;
1265
        ADR_ERR <= not(MMODE);
1266
 
1267
      when others =>
1268
        CSRQ <= (others => '0');
1269
        ADR_ERR <= '1';
1270
 
1271
    end case;
1272
  end process;
1273
 
1274
  process(CLK_i)
1275
  begin
1276
    if(CLK_i = '1' and CLK_i'event) then
1277
      CSRQ_q <= CSRQ;
1278
    end if;
1279
  end process;
1280
 
1281
  -- Illegal instruction exception must be raised if:
1282
  -- 1) an un-existent CSR address is specified, OR
1283
  -- 2) write access is attempted on a read-only CSR, OR
1284
  -- 3) access is attempted on a CSR without appropriate
1285
  -- privileges.
1286
 
1287
  ILLG <= CS_OP_V when (
1288
    ADR_ERR = '1' or -- cases 1,2
1289
    (IADR(9 downto 8) = "11" and MMODE = '0') -- case 3
1290
  ) else '0';
1291
 
1292
  CSRQ_o <= CSRQ;
1293
 
1294
  ILLG_o <= ILLG;
1295
 
1296
  ----------------------------------------------
1297
  -- Control port output
1298
  ----------------------------------------------
1299
 
1300
  process(CP_IADR,MSTATUS_q,MTVEC_q,MIE_q,MTIMECMP_q,MTIME_q2,MTIMEH_q,
1301
    MSCRATCH_q,MEPC_q,MCAUSE_q,MBADADDR_q,MIP_q,MBASE_q,MBOUND_q,
1302
    MIBASE_q,MIBOUND_q,MDBASE_q,MDBOUND_q,MTOHOST_q,MFROMHOST_q,
1303
    UCYCLE_q2,UINSTRET_q2,UCYCLEH_q,UINSTRETH_q,UFCSR_q,UFRM_q,USTATS_q,
1304
    MTDELEG_q,MRV01CC_q,VM
1305
  )
1306
  begin
1307
    case CP_IADR is
1308
 
1309
      -- User mode CSR's
1310
 
1311
      when UCYCLE_ADR =>
1312
        CPQ <= to_signed(UCYCLE_q2,SDLEN);
1313
      when UTIME_ADR =>
1314
        CPQ <= to_signed(UCYCLE_q2,SDLEN);
1315
      when UINSTRET_ADR =>
1316
        CPQ <= to_signed(UINSTRET_q2,SDLEN);
1317
      when UCYCLEH_ADR =>
1318
        CPQ <= to_signed(UCYCLEH_q,SDLEN);
1319
      when UTIMEH_ADR =>
1320
        CPQ <= to_signed(UCYCLEH_q,SDLEN);
1321
      when UINSTRETH_ADR =>
1322
        CPQ <= to_signed(UINSTRETH_q,SDLEN);
1323
      when UFCSR_ADR =>
1324
        CPQ <= UFCSR_q;
1325
      when UFFLAGS_ADR =>
1326
        CPQ(5-1 downto 0) <= UFCSR_q(5-1 downto 0);
1327
        CPQ(SDLEN-1 downto 5) <= (others => '0');
1328
      when UFRM_ADR =>
1329
        CPQ <= UFRM_q;
1330
      when USTATS_ADR =>
1331
        CPQ <= USTATS_q;
1332
 
1333
      -- Machine mode CSR's
1334
 
1335
      when MCPUID_ADR =>
1336
        CPQ <= MCPUID_RO;
1337
      when MIMPID_ADR =>
1338
        CPQ <= MIMPID_RO;
1339
      when MHARTID_ADR =>
1340
        CPQ <= MHARTID_RO;
1341
      when MSTATUS_ADR =>
1342
        CPQ <= MSTATUS_q;
1343
      when MTVEC_ADR =>
1344
        CPQ <= MTVEC_q;
1345
      when MTDELEG_ADR =>
1346
        CPQ <= MTDELEG_q;
1347
      when MIE_ADR =>
1348
        CPQ <= MIE_q;
1349
      when MTIMECMP_ADR =>
1350
        CPQ <= to_signed(MTIMECMP_q,SDLEN);
1351
      when MTIME_ADR =>
1352
        CPQ <= to_signed(MTIME_q2,SDLEN);
1353
      when MTIMEH_ADR =>
1354
        CPQ <= to_signed(MTIMEH_q,SDLEN);
1355
      when MSCRATCH_ADR =>
1356
        CPQ <= MSCRATCH_q;
1357
      when MEPC_ADR =>
1358
        CPQ <= MEPC_q;
1359
      when MCAUSE_ADR =>
1360
        CPQ <= MCAUSE_q;
1361
      when MBADADDR_ADR =>
1362
        CPQ <= MBADADDR_q;
1363
      when MIP_ADR =>
1364
        CPQ <= MIP_q;
1365
      when MBASE_ADR =>
1366
        if(VM = MBBID) then -- is it correct?
1367
          CPQ <= MDBASE_q;
1368
        else
1369
          CPQ <= MBASE_q;
1370
        end if;
1371
      when MBOUND_ADR =>
1372
        if(VM = MBBID) then -- is it correct?
1373
          CPQ <= MDBOUND_q;
1374
        else
1375
          CPQ <= MBOUND_q;
1376
        end if;
1377
      when MIBASE_ADR =>
1378
        CPQ <= MIBASE_q;
1379
      when MIBOUND_ADR =>
1380
        CPQ <= MIBOUND_q;
1381
      when MDBASE_ADR =>
1382
        CPQ <= MDBASE_q;
1383
      when MDBOUND_ADR =>
1384
        CPQ <= MDBOUND_q;
1385
      when MRV01CC_ADR =>
1386
        CPQ <= MRV01CC_q;
1387
 
1388
      -- Host interface CSR's
1389
 
1390
      when MTOHOST_ADR =>
1391
        CPQ <= MTOHOST_q;
1392
      when MFROMHOST_ADR =>
1393
        CPQ <= MFROMHOST_q;
1394
 
1395
      when others =>
1396
        CPQ <= (others => '0');
1397
 
1398
    end case;
1399
  end process;
1400
 
1401
  CPQ_o <= to_std_logic_vector(CPQ);
1402
 
1403
end ARC;

powered by: WebSVN 2.1.0

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