【RISC-V设计-09】- RISC-V处理器设计K0A之CIC

打印 上一主题 下一主题

主题 558|帖子 558|积分 1674

【RISC-V设计-09】- RISC-V处理器设计K0A之CIC


  
1.简介

核内中断控制器(Core Interrupt Controller,简称CIC)是管理和仲裁中断的模块,可以或许根据预设的优先级规则,逐一上报中断请求的序号。本模块具有如下几点功能:

  • 根据全局中断使能,控制中断序号的上报;
  • 根据独立中断使能,控制对应的中断请求;
  • 记载中断状态,根据中断状态向指令译码单元发出中断请求;
  • 根据指令译码单元发出中断应答,清除中断请求;
  • 通过软件、硬件结合的方式,支持晚到中断、咬尾中断;
2.顶层设计


3.端口说明

序号端口位宽方向说明1core_clk1input内核时钟2core_rstn1input内核复位信号,低有效3irq_lines16input外部中断信号,高电平/脉冲触发4csr2cic_gie1input全局中断使能5csr2cic_mie16input独立控制的中断使能6csr2cic_mip16input中断请求等候7cic2csr_irq16output外部中断信号,高电平/脉冲触发8cic2csr_mcause5output中断原因记载9cic2idu_int_req1output向译码单元发送的中断请求10idu2cic_int_ack1input译码单元返回的中断应答11idu2cic_int_mret1input译码单元实行中断返回指令 4.代码设计

  1. // -------------------------------------------------------------------------------------------------
  2. // Copyright 2024 Kearn Chen, kearn.chen@aliyun.com
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. //     http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. // -------------------------------------------------------------------------------------------------
  16. // Description :
  17. //             1. Core Interrupt Controller
  18. // -------------------------------------------------------------------------------------------------
  19. module k0a_core_cic (
  20.     input  wire         core_clk        ,
  21.     input  wire         core_rstn       ,
  22.     input  wire [15:0]  irq_lines       ,
  23.     input  wire         csr2cic_gie     ,
  24.     input  wire [15:0]  csr2cic_mie     ,
  25.     input  wire [15:0]  csr2cic_mip     ,
  26.     output wire [15:0]  cic2csr_irq     ,
  27.     output reg  [4:0]   cic2csr_mcause  ,
  28.     output reg          cic2idu_int_req ,
  29.     input  wire         idu2cic_int_ack ,
  30.     input  wire         idu2cic_int_mret
  31. );
  32. reg     status_irq;
  33. wire cic_int_hit = cic2idu_int_req & idu2cic_int_ack;
  34. wire [15:0] irq_src = csr2cic_mip & csr2cic_mie;
  35. assign cic2csr_irq = irq_lines;
  36. always @(posedge core_clk or negedge core_rstn)
  37. begin
  38.     if(!core_rstn)
  39.         status_irq <= 1'b0;
  40.     else if(cic_int_hit)
  41.         status_irq <= 1'b1;
  42.     else if(idu2cic_int_mret)
  43.         status_irq <= 1'b0;
  44. end
  45. always @(posedge core_clk)
  46. begin
  47.     if(csr2cic_gie) begin
  48.         case(1'b1)
  49.             irq_src[ 0] : cic2csr_mcause <= {1'b1, 4'h0};
  50.             irq_src[ 1] : cic2csr_mcause <= {1'b1, 4'h1};
  51.             irq_src[ 2] : cic2csr_mcause <= {1'b1, 4'h2};
  52.             irq_src[ 3] : cic2csr_mcause <= {1'b1, 4'h3};
  53.             irq_src[ 4] : cic2csr_mcause <= {1'b1, 4'h4};
  54.             irq_src[ 5] : cic2csr_mcause <= {1'b1, 4'h5};
  55.             irq_src[ 6] : cic2csr_mcause <= {1'b1, 4'h6};
  56.             irq_src[ 7] : cic2csr_mcause <= {1'b1, 4'h7};
  57.             irq_src[ 8] : cic2csr_mcause <= {1'b1, 4'h8};
  58.             irq_src[ 9] : cic2csr_mcause <= {1'b1, 4'h9};
  59.             irq_src[10] : cic2csr_mcause <= {1'b1, 4'ha};
  60.             irq_src[11] : cic2csr_mcause <= {1'b1, 4'hb};
  61.             irq_src[12] : cic2csr_mcause <= {1'b1, 4'hc};
  62.             irq_src[13] : cic2csr_mcause <= {1'b1, 4'hd};
  63.             irq_src[14] : cic2csr_mcause <= {1'b1, 4'he};
  64.             irq_src[15] : cic2csr_mcause <= {1'b1, 4'hf};
  65.             default     : cic2csr_mcause <= 5'd0;
  66.         endcase
  67.     end
  68. end
  69. always @(posedge core_clk or negedge core_rstn)
  70. begin
  71.     if(!core_rstn)
  72.         cic2idu_int_req <= 1'b0;
  73.     else if(cic_int_hit)
  74.         cic2idu_int_req <= 1'b0;
  75.     else if(~status_irq & (|irq_src))
  76.         cic2idu_int_req <= 1'b1;
  77. end
  78. endmodule
复制代码
5.仲裁代码

  1. # -------------------------------------------------------------------------------------------------
  2. # Copyright 2024 Kearn Chen, kearn.chen@aliyun.com
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. #     http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. # -------------------------------------------------------------------------------------------------
  16.         .section .init, "ax", @progbits
  17.         .globl  _start
  18.         .align  2
  19. _start:
  20.         .option norvc;
  21.     j       handler_reset
  22.     .word   handler_irq0
  23.     .word   handler_irq1
  24.     .word   handler_irq2
  25.     .word   handler_irq3
  26.     .word   handler_irq4
  27.     .word   handler_irq5
  28.     .word   handler_irq6
  29.     .word   handler_irq7
  30.     .word   handler_irq8
  31.     .word   handler_irq9
  32.     .word   handler_irq10
  33.     .word   handler_irq11
  34.     .word   handler_irq12
  35.     .word   handler_irq13
  36.     .word   handler_irq14
  37.     .word   handler_irq15
  38.         .section .text.handler_isr, "ax", @progbits
  39.         .align    2
  40. handler_isr:
  41.     addi sp, sp, -56
  42.     sw x1 , 0(sp)
  43.     sw x4 , 4(sp)
  44.     sw x5 , 8(sp)
  45.     sw x6 , 12(sp)
  46.     sw x7 , 16(sp)
  47.     sw x8 , 20(sp)
  48.     sw x9 , 24(sp)
  49.     sw x10, 28(sp)
  50.     sw x11, 32(sp)
  51.     sw x12, 36(sp)
  52.     sw x13, 40(sp)
  53.     sw x14, 44(sp)
  54.     sw x15, 48(sp)
  55. 1:
  56.     csrr a0, mcause
  57.     beq x0, a0, 2f
  58.     andi a0, a0, 15
  59.     addi a1, x0, 1
  60.     sll a2, a1, a0
  61.     csrrc a2, mip, a2
  62.     slli a0, a0, 2
  63.     lw a1, 4(a0)
  64.     jalr ra, a1, 0
  65.     jal x0, 1b
  66. 2:
  67.     lw x15, 48(sp)
  68.     lw x14, 44(sp)
  69.     lw x13, 40(sp)
  70.     lw x12, 36(sp)
  71.     lw x11, 32(sp)
  72.     lw x10, 28(sp)
  73.     lw x9 , 24(sp)
  74.     lw x8 , 20(sp)
  75.     lw x7 , 16(sp)
  76.     lw x6 , 12(sp)
  77.     lw x5 , 8(sp)
  78.     lw x4 , 4(sp)
  79.     lw x1 , 0(sp)
  80.     addi sp, sp, 56
  81.     mret
  82.         .section .text.handler_default, "ax", @progbits
  83.         .align    2
  84.         .weak   handler_irq0
  85.         .weak   handler_irq1
  86.         .weak   handler_irq2
  87.         .weak   handler_irq3
  88.         .weak   handler_irq4
  89.         .weak   handler_irq5
  90.         .weak   handler_irq6
  91.         .weak   handler_irq7
  92.         .weak   handler_irq8
  93.         .weak   handler_irq9
  94.         .weak   handler_irq10
  95.         .weak   handler_irq11
  96.         .weak   handler_irq12
  97.         .weak   handler_irq13
  98.         .weak   handler_irq14
  99.         .weak   handler_irq15
  100. handler_irq0:
  101. handler_irq1:
  102. handler_irq2:
  103. handler_irq3:
  104. handler_irq4:
  105. handler_irq5:
  106. handler_irq6:
  107. handler_irq7:
  108. handler_irq8:
  109. handler_irq9:
  110. handler_irq10:
  111. handler_irq11:
  112. handler_irq12:
  113. handler_irq13:
  114. handler_irq14:
  115. handler_irq15:
  116. loop_isr:
  117.         j loop_isr
  118.         .section  .text.handler_reset, "ax", @progbits
  119.         .align    2
  120. handler_reset:
  121. .option push
  122. .option norelax
  123.         la gp, __global_pointer$
  124. .option pop
  125. 1:
  126.         la sp, _eusrstack
  127. 2:
  128.         la a0, _data_lma
  129.         la a1, _data_vma
  130.         la a2, _edata
  131.         bgeu a1, a2, 2f
  132. 1:
  133.         lw a3, 0(a0)
  134.         sw a3, 0(a1)
  135.         addi a0, a0, 4
  136.         addi a1, a1, 4
  137.         bgeu a1, a2, 2f
  138.         lw a3, 0(a0)
  139.         sw a3, 0(a1)
  140.         addi a0, a0, 4
  141.         addi a1, a1, 4
  142.         bltu a1, a2, 1b
  143. 2:
  144.     la a0, _sbss
  145.     la a1, _ebss
  146.     bgeu a0, a1, 2f
  147. 1:
  148.     sw x0, 0(a0)
  149.     addi a0, a0, 4
  150.     bgeu a0, a1, 2f
  151.     sw x0, 0(a0)
  152.     addi a0, a0, 4
  153.     bgeu a0, a1, 2f
  154.     sw x0, 0(a0)
  155.     addi a0, a0, 4
  156.     bltu a0, a1, 1b
  157. 2:
  158.     la a3, handler_isr
  159.     csrw mtvec, a3
  160.     j main
复制代码
这段代码是开始运行时初始化的代码,同时还存在中断处理干系的操纵,通过读取中断原因、进行一些位操纵确定中断序号。如果中断原因不为 0,根据中断序号查找到中断向量表,跳转到特定的地址实行相应的中断处理程序。
6.总结

本文阐述了一种极为简洁的中断控制模块。该模块采用软件与硬件相结合的方式,来达成中断的处理。而且,它还可以或许支持晚到中断和咬尾中断,一次收支栈就可以或许处置多个现存的中断。如此一来,节省了中断处理的时间,进步了中断处理的效率。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

圆咕噜咕噜

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表