SAIFIINDUSTRIES commited on
Commit
0693bdd
·
verified ·
1 Parent(s): 49812da

Add Batch 2 with 10 repos

Browse files
This view is limited to 50 files because it contains too many changes.   See raw diff
Files changed (50) hide show
  1. .gitattributes +2 -0
  2. CutestPanda_Opensoc/100_apb_gpio/firmware/apb_gpio.c +107 -0
  3. CutestPanda_Opensoc/100_apb_gpio/firmware/apb_gpio.h +42 -0
  4. CutestPanda_Opensoc/100_apb_gpio/firmware/examples/apb_gpio_in_itr_example.c +55 -0
  5. CutestPanda_Opensoc/100_apb_gpio/firmware/examples/apb_gpio_out_example.c +45 -0
  6. CutestPanda_Opensoc/100_apb_gpio/firmware/examples/delay.c +17 -0
  7. CutestPanda_Opensoc/100_apb_gpio/firmware/examples/delay.h +5 -0
  8. CutestPanda_Opensoc/100_apb_gpio/rtl/apb_gpio.v +314 -0
  9. CutestPanda_Opensoc/100_apb_gpio/rtl/itr_generator.v +104 -0
  10. CutestPanda_Opensoc/101_apb_uart/firmware/apb_uart.c +153 -0
  11. CutestPanda_Opensoc/101_apb_uart/firmware/apb_uart.h +60 -0
  12. CutestPanda_Opensoc/101_apb_uart/firmware/examples/apb_uart_rx_itr_example.c +55 -0
  13. CutestPanda_Opensoc/101_apb_uart/firmware/examples/apb_uart_tx_example.c +28 -0
  14. CutestPanda_Opensoc/101_apb_uart/rtl/apb_uart.v +617 -0
  15. CutestPanda_Opensoc/101_apb_uart/rtl/bram_simple_dual_port.v +132 -0
  16. CutestPanda_Opensoc/101_apb_uart/rtl/dram_simple_dual_port.v +129 -0
  17. CutestPanda_Opensoc/101_apb_uart/rtl/fifo_base_on_ram.v +200 -0
  18. CutestPanda_Opensoc/101_apb_uart/rtl/fifo_based_on_lutram.v +235 -0
  19. CutestPanda_Opensoc/101_apb_uart/rtl/fifo_based_on_ram_std.v +223 -0
  20. CutestPanda_Opensoc/101_apb_uart/rtl/fifo_show_ahead_buffer.v +122 -0
  21. CutestPanda_Opensoc/101_apb_uart/rtl/itr_generator.v +104 -0
  22. CutestPanda_Opensoc/101_apb_uart/rtl/ram_fifo_wrapper.v +220 -0
  23. CutestPanda_Opensoc/101_apb_uart/rtl/uart_rx.v +263 -0
  24. CutestPanda_Opensoc/101_apb_uart/rtl/uart_rx_tx.v +138 -0
  25. CutestPanda_Opensoc/101_apb_uart/rtl/uart_tx.v +226 -0
  26. CutestPanda_Opensoc/102_apb_i2c/firmware/apb_i2c.c +187 -0
  27. CutestPanda_Opensoc/102_apb_i2c/firmware/apb_i2c.h +44 -0
  28. CutestPanda_Opensoc/102_apb_i2c/firmware/examples/apb_i2c_eeprom_example.c +101 -0
  29. CutestPanda_Opensoc/102_apb_i2c/firmware/examples/delay.c +17 -0
  30. CutestPanda_Opensoc/102_apb_i2c/firmware/examples/delay.h +5 -0
  31. CutestPanda_Opensoc/102_apb_i2c/rtl/apb_i2c.v +285 -0
  32. CutestPanda_Opensoc/102_apb_i2c/rtl/bram_simple_dual_port.v +132 -0
  33. CutestPanda_Opensoc/102_apb_i2c/rtl/dram_simple_dual_port.v +129 -0
  34. CutestPanda_Opensoc/102_apb_i2c/rtl/fifo_base_on_ram.v +200 -0
  35. CutestPanda_Opensoc/102_apb_i2c/rtl/fifo_based_on_lutram.v +235 -0
  36. CutestPanda_Opensoc/102_apb_i2c/rtl/fifo_based_on_ram_std.v +223 -0
  37. CutestPanda_Opensoc/102_apb_i2c/rtl/fifo_show_ahead_buffer.v +122 -0
  38. CutestPanda_Opensoc/102_apb_i2c/rtl/i2c_ctrler.v +363 -0
  39. CutestPanda_Opensoc/102_apb_i2c/rtl/i2c_master_if.v +358 -0
  40. CutestPanda_Opensoc/102_apb_i2c/rtl/itr_generator.v +104 -0
  41. CutestPanda_Opensoc/102_apb_i2c/rtl/ram_fifo_wrapper.v +220 -0
  42. CutestPanda_Opensoc/102_apb_i2c/rtl/regs_if_for_i2c.v +307 -0
  43. CutestPanda_Opensoc/103_generic_fsmc_ctrler/firmware/examples/fsmc_example.c +61 -0
  44. CutestPanda_Opensoc/103_generic_fsmc_ctrler/firmware/generic_fsmc_ctrler.c +99 -0
  45. CutestPanda_Opensoc/103_generic_fsmc_ctrler/firmware/generic_fsmc_ctrler.h +25 -0
  46. CutestPanda_Opensoc/103_generic_fsmc_ctrler/rtl/ahb_fsmc.v +229 -0
  47. CutestPanda_Opensoc/103_generic_fsmc_ctrler/rtl/axi_fsmc.v +368 -0
  48. CutestPanda_Opensoc/103_generic_fsmc_ctrler/rtl/fsmc_ctrler.v +210 -0
  49. CutestPanda_Opensoc/103_generic_fsmc_ctrler/rtl/round_robin_arbitrator.v +105 -0
  50. CutestPanda_Opensoc/103_generic_fsmc_ctrler/tb/axi_fsmc/agents.sv +413 -0
.gitattributes CHANGED
@@ -81,3 +81,5 @@ The-OpenROAD-Project_OpenROAD-flow-scripts/flow/designs/nangate45/bp_quad/bsg_ch
81
  The-OpenROAD-Project_OpenROAD-flow-scripts/flow/designs/src/bp_quad/bsg_chip_block.sv2v.v filter=lfs diff=lfs merge=lfs -text
82
  The-OpenROAD-Project_OpenROAD-flow-scripts/flow/designs/src/coyote/coyote.sv2v.v filter=lfs diff=lfs merge=lfs -text
83
  OpenTimer_OpenTimer/benchmark/vga_lcd/vga_lcd.v filter=lfs diff=lfs merge=lfs -text
 
 
 
81
  The-OpenROAD-Project_OpenROAD-flow-scripts/flow/designs/src/bp_quad/bsg_chip_block.sv2v.v filter=lfs diff=lfs merge=lfs -text
82
  The-OpenROAD-Project_OpenROAD-flow-scripts/flow/designs/src/coyote/coyote.sv2v.v filter=lfs diff=lfs merge=lfs -text
83
  OpenTimer_OpenTimer/benchmark/vga_lcd/vga_lcd.v filter=lfs diff=lfs merge=lfs -text
84
+ hguq_HG-PIPE/SPINAL/BlockSequence_bb.v filter=lfs diff=lfs merge=lfs -text
85
+ hguq_HG-PIPE/SPINAL/vivado/BlockSequence_bb_replaced.v filter=lfs diff=lfs merge=lfs -text
CutestPanda_Opensoc/100_apb_gpio/firmware/apb_gpio.c ADDED
@@ -0,0 +1,107 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /************************************************************************************************************************
2
+ APB-GPIO����(��Դ�ļ�)
3
+ @brief APB-GPIO����
4
+ @date 2024/08/23
5
+ @author �¼�ҫ
6
+ @eidt 2024/08/23 1.00 �����˵�һ����ʽ�汾
7
+ ************************************************************************************************************************/
8
+
9
+ #include "apb_gpio.h"
10
+
11
+ ////////////////////////////////////////////////////////////////////////////////////////////////////////////
12
+
13
+ /*************************
14
+ @init
15
+ @public
16
+ @brief ��ʼ��APB-GPIO
17
+ @param apb_gpio APB-GPIO(�ṹ��ָ��)
18
+ base_addr APB-GPIO�������ַ
19
+ @return none
20
+ *************************/
21
+ void apb_gpio_init(ApbGPIO* apb_gpio, uint32_t base_addr){
22
+ apb_gpio->hardware = (ApbGPIOHd*)base_addr;
23
+ }
24
+
25
+ /*************************
26
+ @io
27
+ @public
28
+ @brief APB-GPIOд��ƽ
29
+ @param apb_gpio APB-GPIO(�ṹ��ָ��)
30
+ mask д��ƽ����
31
+ value ��д��ƽֵ
32
+ @return none
33
+ *************************/
34
+ void apb_gpio_write_pin(ApbGPIO* apb_gpio, uint32_t mask, uint32_t value){
35
+ apb_gpio->hardware->out_mask = mask;
36
+ apb_gpio->hardware->out_v = value;
37
+ }
38
+
39
+ /*************************
40
+ @io
41
+ @public
42
+ @brief APB-GPIO����ƽ
43
+ @param apb_gpio APB-GPIO(�ṹ��ָ��)
44
+ @return ��ȡ���ĵ�ƽֵ
45
+ *************************/
46
+ uint32_t apb_gpio_read_pin(ApbGPIO* apb_gpio){
47
+ return apb_gpio->hardware->in_v;
48
+ }
49
+
50
+ /*************************
51
+ @io
52
+ @public
53
+ @brief APB-GPIO������̬�ŷ���
54
+ @param apb_gpio APB-GPIO(�ṹ��ָ��)
55
+ dire ��������(0��ʾ���, 1��ʾ����)
56
+ @return none
57
+ *************************/
58
+ void apb_gpio_set_direction(ApbGPIO* apb_gpio, uint32_t dire){
59
+ apb_gpio->now_dire = dire;
60
+ apb_gpio->hardware->dire = dire;
61
+ }
62
+
63
+ /*************************
64
+ @cfg
65
+ @public
66
+ @brief APB-GPIOʹ���ж�
67
+ @param apb_gpio APB-GPIO(�ṹ��ָ��)
68
+ itr_en �ж�ʹ������
69
+ @return none
70
+ *************************/
71
+ void apb_gpio_enable_itr(ApbGPIO* apb_gpio, uint32_t itr_en){
72
+ apb_gpio->hardware->itr_global = 0x00000001;
73
+ apb_gpio->hardware->itr_en = itr_en;
74
+ }
75
+
76
+ /*************************
77
+ @cfg
78
+ @public
79
+ @brief APB-GPIO�����ж�
80
+ @param apb_gpio APB-GPIO(�ṹ��ָ��)
81
+ @return none
82
+ *************************/
83
+ void apb_gpio_disable_itr(ApbGPIO* apb_gpio){
84
+ apb_gpio->hardware->itr_global = 0x00000000;
85
+ }
86
+
87
+ /*************************
88
+ @sts
89
+ @public
90
+ @brief APB-GPIO��ȡ�ж�״̬
91
+ @param apb_gpio APB-GPIO(�ṹ��ָ��)
92
+ @return �ж�״̬����
93
+ *************************/
94
+ uint32_t apb_gpio_get_itr_status(ApbGPIO* apb_gpio){
95
+ return apb_gpio->hardware->itr_status;
96
+ }
97
+
98
+ /*************************
99
+ @cfg
100
+ @public
101
+ @brief APB-GPIO����жϱ�־
102
+ @param apb_gpio APB-GPIO(�ṹ��ָ��)
103
+ @return none
104
+ *************************/
105
+ void apb_gpio_clear_itr_flag(ApbGPIO* apb_gpio){
106
+ apb_gpio->hardware->itr_global = 0x00000001;
107
+ }
CutestPanda_Opensoc/100_apb_gpio/firmware/apb_gpio.h ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /************************************************************************************************************************
2
+ APB-GPIO����(�ӿ�ͷ�ļ�)
3
+ @brief APB-GPIO����
4
+ @date 2024/08/23
5
+ @author �¼�ҫ
6
+ ************************************************************************************************************************/
7
+
8
+ #include <stdint.h>
9
+
10
+ ////////////////////////////////////////////////////////////////////////////////////////////////////////////
11
+
12
+ #ifndef __APB_GPIO_H
13
+ #define __APB_GPIO_H
14
+
15
+ typedef struct{
16
+ uint32_t out_v; // GPIO���
17
+ uint32_t out_mask; // GPIOд��ƽ����
18
+ uint32_t dire; // GPIO����(0��ʾ���, 1��ʾ����)
19
+ uint32_t in_v; // GPIO����
20
+ uint32_t itr_global; // ȫ���ж�ʹ��, ȫ���жϱ�־
21
+ uint32_t itr_status; // �ж�״̬
22
+ uint32_t itr_en; // �ж�ʹ��
23
+ }ApbGPIOHd;
24
+
25
+ typedef struct{
26
+ ApbGPIOHd* hardware; // APB-GPIO�Ĵ����ӿ�(�ṹ��ָ��)
27
+ uint32_t now_dire; // ��ǰ��GPIO����(0��ʾ���, 1��ʾ����)
28
+ }ApbGPIO;
29
+
30
+ #endif
31
+
32
+ ////////////////////////////////////////////////////////////////////////////////////////////////////////////
33
+
34
+ void apb_gpio_init(ApbGPIO* apb_gpio, uint32_t base_addr); // ��ʼ��APB-GPIO
35
+ void apb_gpio_write_pin(ApbGPIO* apb_gpio, uint32_t mask, uint32_t value); // APB-GPIOд��ƽ
36
+ uint32_t apb_gpio_read_pin(ApbGPIO* apb_gpio); // APB-GPIO����ƽ
37
+ void apb_gpio_set_direction(ApbGPIO* apb_gpio, uint32_t dire); // APB-GPIO������̬�ŷ���
38
+
39
+ void apb_gpio_enable_itr(ApbGPIO* apb_gpio, uint32_t itr_en); // APB-GPIOʹ���ж�
40
+ void apb_gpio_disable_itr(ApbGPIO* apb_gpio); // APB-GPIO�����ж�
41
+ uint32_t apb_gpio_get_itr_status(ApbGPIO* apb_gpio); // APB-GPIO��ȡ�ж�״̬
42
+ void apb_gpio_clear_itr_flag(ApbGPIO* apb_gpio); // APB-GPIO����жϱ�־
CutestPanda_Opensoc/100_apb_gpio/firmware/examples/apb_gpio_in_itr_example.c ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /************************************************************************************************************************
2
+ APB-GPIOʾ������
3
+ @brief GPIO�����ж�ʾ��
4
+ @attention �����Ӳ��ƽ̨������ȫ���жϿ�������ص�API
5
+ @date 2024/08/23
6
+ @author �¼�ҫ
7
+ @eidt 2024/08/23 1.00 �����˵�һ����ʽ�汾
8
+ ************************************************************************************************************************/
9
+
10
+ #include "../apb_gpio.h"
11
+
12
+ #include "CMSDK_CM0.h"
13
+
14
+ ////////////////////////////////////////////////////////////////////////////////////////////////////////////
15
+
16
+ #define APB_GPIO_BASEADDR 0x40000000 // APB-GPIO����ַ
17
+
18
+ ////////////////////////////////////////////////////////////////////////////////////////////////////////////
19
+
20
+ static ApbGPIO gpio; // APB-GPIO����ṹ��
21
+
22
+ static uint8_t led_pin_value = 0x00; // 1��ͨ����ǰ�������ƽֵ
23
+
24
+ ////////////////////////////////////////////////////////////////////////////////////////////////////////////
25
+
26
+ /*************************
27
+ @itr_handler
28
+ @private
29
+ @brief APB-GPIO�����жϷ������(ʾ��)
30
+ @param none
31
+ @return none
32
+ *************************/
33
+ void USER_GPIO_Handler(void){
34
+ // ��ת1��ͨ���������ƽ
35
+ led_pin_value = !led_pin_value;
36
+ apb_gpio_write_pin(&gpio, 0x00000002, led_pin_value ? 0xFFFFFFFF:0x00000000);
37
+
38
+ apb_gpio_clear_itr_flag(&gpio); // ����жϱ�־
39
+ }
40
+
41
+ ////////////////////////////////////////////////////////////////////////////////////////////////////////////
42
+
43
+ void apb_gpio_in_itr_toggle_example(void){
44
+ apb_gpio_init(&gpio, APB_GPIO_BASEADDR); // ��ʼ��APB-GPIO
45
+
46
+ apb_gpio_set_direction(&gpio, 0xFFFFFFFD); // ����0��ͨ������Ϊ����, 1��ͨ������Ϊ���
47
+ apb_gpio_write_pin(&gpio, 0x00000002, 0x00000000); // ����1��ͨ������͵�ƽ
48
+
49
+ NVIC_SetPriority((IRQn_Type)3, 0x03); // NVIC����3���ж����ȼ�
50
+ NVIC_EnableIRQ((IRQn_Type)3); // NVICʹ��3���ж�
51
+
52
+ apb_gpio_enable_itr(&gpio, 0x00000001); // APB-GPIOʹ��0��ͨ���������ж�
53
+
54
+ while(1);
55
+ }
CutestPanda_Opensoc/100_apb_gpio/firmware/examples/apb_gpio_out_example.c ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /************************************************************************************************************************
2
+ APB-GPIOʾ������
3
+ @brief ����APB-GPIOʵ����ˮ��
4
+ ��ˮ�ư���GPIO�����0~5��ͨ��
5
+ @attention �����Ӳ��ƽ̨�������ӳ�(delay)��ص�API
6
+ @date 2024/08/23
7
+ @author �¼�ҫ
8
+ @eidt 2024/08/23 1.00 �����˵�һ����ʽ�汾
9
+ ************************************************************************************************************************/
10
+
11
+ #include "../apb_gpio.h"
12
+ #include "delay.h"
13
+
14
+ ////////////////////////////////////////////////////////////////////////////////////////////////////////////
15
+
16
+ #define APB_GPIO_BASEADDR 0x40000000 // APB-GPIO����ַ
17
+
18
+ ////////////////////////////////////////////////////////////////////////////////////////////////////////////
19
+
20
+ const static uint8_t flow_led_out_value[7] = {0x20, 0x10, 0x08, 0x04, 0x02, 0x01, 0x00}; // ��ˮ����ʽ
21
+
22
+ static ApbGPIO gpio; // APB-GPIO����ṹ��
23
+ static uint8_t flow_led_stage_cnt = 0; // ��ˮ�ƽ׶μ�����
24
+
25
+ ////////////////////////////////////////////////////////////////////////////////////////////////////////////
26
+
27
+ void apb_gpio_flow_led_example(void){
28
+ apb_gpio_init(&gpio, APB_GPIO_BASEADDR); // ��ʼ��APB-GPIO
29
+
30
+ apb_gpio_set_direction(&gpio, 0xFFFFFFC0); // ����0~5��ͨ������Ϊ���
31
+ apb_gpio_write_pin(&gpio, 0x0000003F, 0x00000000); // ����0~5��ͨ������͵�ƽ
32
+
33
+ while(1){
34
+ apb_gpio_write_pin(&gpio, 0x0000003F, (uint32_t)flow_led_out_value[flow_led_stage_cnt]); // �����ˮ�Ƶ�ƽ
35
+
36
+ // ������ˮ�ƽ׶μ�����
37
+ if(flow_led_stage_cnt == 6){
38
+ flow_led_stage_cnt = 0;
39
+ }else{
40
+ flow_led_stage_cnt++;
41
+ }
42
+
43
+ delay_ms(500); // �ӳ�0.5s
44
+ }
45
+ }
CutestPanda_Opensoc/100_apb_gpio/firmware/examples/delay.c ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #include "delay.h"
2
+
3
+ ////////////////////////////////////////////////////////////////////////////////////////////////////////////
4
+
5
+ static uint32_t cnt = 0;
6
+
7
+ ////////////////////////////////////////////////////////////////////////////////////////////////////////////
8
+
9
+ void SysTick_Handler(void){
10
+ cnt++;
11
+ }
12
+
13
+ void delay_ms(uint32_t t){
14
+ cnt = 0;
15
+
16
+ while(cnt != t);
17
+ }
CutestPanda_Opensoc/100_apb_gpio/firmware/examples/delay.h ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ #include <stdint.h>
2
+
3
+ ////////////////////////////////////////////////////////////////////////////////////////////////////////////
4
+
5
+ void delay_ms(uint32_t t);
CutestPanda_Opensoc/100_apb_gpio/rtl/apb_gpio.v ADDED
@@ -0,0 +1,314 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ MIT License
3
+
4
+ Copyright (c) 2024 Panda, 2257691535@qq.com
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
23
+ */
24
+
25
+ `timescale 1ns / 1ps
26
+ /********************************************************************
27
+ 本模块: 符合APB协议的GPIO控制器
28
+
29
+ 描述:
30
+ APB-GPIO控制器
31
+ 支持GPIO输入中断
32
+
33
+ 寄存器->
34
+ 偏移量 | 含义 | 读写特性 | 备注
35
+ 0x00 gpio_width-1~0:GPIO输出 W
36
+ 0x04 gpio_width-1~0:GPIO写电平掩码 W
37
+ 0x08 gpio_width-1~0:GPIO方向 W 0为输出, 1为输入
38
+ 0x0C gpio_width-1~0:GPIO输入 R
39
+ 0x10 0:全局中断使能 W
40
+ 1:全局中断标志 RWC 请在中断服务函数中清除中断标志
41
+ 0x14 gpio_width-1~0:中断状态 R
42
+ 0x18 gpio_width-1~0:中断使能 W
43
+
44
+ 注意:
45
+
46
+
47
+ 协议:
48
+ APB SLAVE
49
+ GPIO
50
+
51
+ 作者: 陈家耀
52
+ 日期: 2023/11/06
53
+ ********************************************************************/
54
+
55
+
56
+ module apb_gpio #(
57
+ parameter integer gpio_width = 16, // GPIO位宽(1~32)
58
+ parameter gpio_dire = "inout", // GPIO方向(inout|input|output)
59
+ parameter default_output_value = 32'hffff_ffff, // GPIO默认输出电平
60
+ parameter default_tri_value = 32'hffff_ffff, // GPIO默认方向(0->输出 1->输入)(仅在inout模式下可用)
61
+ parameter en_itr = "true", // 是否使能GPIO中断
62
+ parameter itr_edge = "neg", // 中断极性(pos|neg)
63
+ parameter real simulation_delay = 1 // 仿真延时
64
+ )(
65
+ // 时钟和复位
66
+ input wire clk,
67
+ input wire resetn,
68
+
69
+ // APB从机接口
70
+ input wire[31:0] paddr,
71
+ input wire psel,
72
+ input wire penable,
73
+ input wire pwrite,
74
+ input wire[31:0] pwdata,
75
+ output wire pready_out, // const -> 1'b1
76
+ output wire[31:0] prdata_out,
77
+ output wire pslverr_out, // const -> 1'b0
78
+
79
+ // GPIO
80
+ output wire[gpio_width-1:0] gpio_o,
81
+ output wire[gpio_width-1:0] gpio_t, // 0->输出, 1->输入
82
+ input wire[gpio_width-1:0] gpio_i,
83
+
84
+ // 中断
85
+ output wire gpio_itr
86
+ );
87
+
88
+ /** GPIO三态总线 **/
89
+ wire[gpio_width-1:0] gpio_o_w;
90
+ wire[gpio_width-1:0] gpio_t_w;
91
+ wire[gpio_width-1:0] gpio_i_w;
92
+
93
+ generate
94
+ if(gpio_dire != "input")
95
+ assign gpio_o = gpio_o_w;
96
+ else
97
+ assign gpio_o = default_output_value;
98
+
99
+ if(gpio_dire == "inout")
100
+ assign gpio_t = gpio_t_w;
101
+ else if(gpio_dire == "input")
102
+ assign gpio_t = 32'hffff_ffff;
103
+ else
104
+ assign gpio_t = 32'h0000_0000;
105
+
106
+ if(gpio_dire != "output")
107
+ begin
108
+ reg[gpio_width-1:0] gpio_i_d;
109
+ reg[gpio_width-1:0] gpio_i_d2;
110
+
111
+ assign gpio_i_w = gpio_i_d2;
112
+
113
+ always @(posedge clk)
114
+ begin
115
+ # simulation_delay;
116
+
117
+ gpio_i_d <= gpio_i;
118
+ gpio_i_d2 <= gpio_i_d;
119
+ end
120
+ end
121
+ else
122
+ assign gpio_i_w = gpio_o_w;
123
+ endgenerate
124
+
125
+ /** GPIO中断 **/
126
+ wire[gpio_width-1:0] gpio_i_w_d; // 延迟1clk的gpio输入
127
+ wire gpio_global_itr_en_w; // 全局中断使能
128
+ wire[gpio_width-1:0] gpio_itr_en_w; // 中断使能
129
+ wire gpio_itr_flag_w; // 中断标志
130
+ wire[gpio_width-1:0] gpio_itr_mask_w; // 中断掩码
131
+ wire[gpio_width-1:0] gpio_itr_mask_w_d; // 延迟1clk的中断掩码
132
+ wire gpio_org_itr_pulse; // 原始中断脉冲
133
+ wire gpio_itr_w; // 中断信号
134
+
135
+ assign gpio_itr = gpio_itr_w;
136
+
137
+ generate
138
+ if((en_itr == "true") && (gpio_dire != "output"))
139
+ begin
140
+ reg[gpio_width-1:0] gpio_org_itr_mask_regs;
141
+ reg[gpio_width-1:0] gpio_itr_mask_regs_d;
142
+ reg gpio_org_itr_pulse_reg;
143
+
144
+ assign gpio_itr_mask_w = gpio_org_itr_mask_regs;
145
+ assign gpio_itr_mask_w_d = gpio_itr_mask_regs_d;
146
+ assign gpio_org_itr_pulse = gpio_org_itr_pulse_reg;
147
+
148
+ always @(posedge clk or negedge resetn)
149
+ begin
150
+ if(~resetn)
151
+ begin
152
+ gpio_org_itr_pulse_reg <= 1'b0;
153
+ gpio_org_itr_mask_regs <= {gpio_width{1'b0}};
154
+ gpio_itr_mask_regs_d <= {gpio_width{1'b0}};
155
+ end
156
+ else
157
+ begin
158
+ # simulation_delay;
159
+
160
+ gpio_org_itr_pulse_reg <= (gpio_org_itr_mask_regs != {gpio_width{1'b0}}) & gpio_global_itr_en_w & (~gpio_itr_flag_w); // 原始中断脉冲
161
+ gpio_org_itr_mask_regs <= ((itr_edge == "pos") ? (gpio_i_w & (~gpio_i_w_d)):((~gpio_i_w) & gpio_i_w_d)) & // 捕获上升沿或下降沿
162
+ gpio_t_w & // 仅考虑输入模式gpio的中断
163
+ gpio_itr_en_w; // gpio输入中断使能
164
+ gpio_itr_mask_regs_d <= gpio_org_itr_mask_regs; // 对中断状态打1拍
165
+ end
166
+ end
167
+
168
+ // 对原始中断脉冲进行延展
169
+ itr_generator #(
170
+ .pulse_w(10),
171
+ .simulation_delay(simulation_delay)
172
+ )itr_generator_u(
173
+ .clk(clk),
174
+ .rst_n(resetn),
175
+ .itr_org(gpio_org_itr_pulse),
176
+ .itr(gpio_itr_w)
177
+ );
178
+ end
179
+ else
180
+ begin
181
+ assign gpio_itr_w = 1'b0;
182
+
183
+ assign gpio_itr_mask_w = 32'd0;
184
+ assign gpio_org_itr_pulse = 1'b0;
185
+ end
186
+ endgenerate
187
+
188
+ /** APB写寄存器 **/
189
+ reg[31:0] gpio_o_value_regs; // GPIO输出
190
+ reg[31:0] gpio_o_mask_regs; // GPIO写电平掩码
191
+ reg[31:0] gpio_direction_regs; // GPIO方向
192
+ reg[31:0] gpio_i_value_regs; // GPIO输入
193
+ reg[31:0] gpio_itr_status_regs; // 全局中断使能, 中断标志
194
+ reg[31:0] gpio_itr_mask_regs; // 中断状态
195
+ reg[31:0] gpio_itr_en_regs; // 中断使能
196
+
197
+ assign gpio_o_w = gpio_o_value_regs[gpio_width-1:0];
198
+ assign gpio_t_w = gpio_direction_regs[gpio_width-1:0];
199
+
200
+ assign gpio_i_w_d = gpio_i_value_regs[gpio_width-1:0];
201
+ assign gpio_global_itr_en_w = gpio_itr_status_regs[0];
202
+ assign gpio_itr_en_w = gpio_itr_en_regs;
203
+ assign gpio_itr_flag_w = gpio_itr_status_regs[1];
204
+
205
+ genvar gpio_o_value_regs_i;
206
+ generate
207
+ for(gpio_o_value_regs_i = 0;gpio_o_value_regs_i < gpio_width;gpio_o_value_regs_i = gpio_o_value_regs_i + 1)
208
+ begin
209
+ always @(posedge clk or negedge resetn)
210
+ begin
211
+ if(~resetn)
212
+ gpio_o_value_regs[gpio_o_value_regs_i] <= default_output_value[gpio_o_value_regs_i];
213
+ else if(psel & pwrite & penable & (paddr[4:2] == 3'd0) & gpio_o_mask_regs[gpio_o_value_regs_i]) // 写GPIO输出电平
214
+ # simulation_delay gpio_o_value_regs[gpio_o_value_regs_i] <= pwdata[gpio_o_value_regs_i];
215
+ end
216
+ end
217
+ endgenerate
218
+
219
+ always @(posedge clk or negedge resetn)
220
+ begin
221
+ if(~resetn)
222
+ begin
223
+ gpio_o_mask_regs[gpio_width-1:0] <= {gpio_width{1'b1}};
224
+
225
+ if(gpio_dire == "inout")
226
+ gpio_direction_regs[gpio_width-1:0] <= default_tri_value;
227
+ else if(gpio_dire == "input")
228
+ gpio_direction_regs[gpio_width-1:0] <= {gpio_width{1'b1}};
229
+ else
230
+ gpio_direction_regs[gpio_width-1:0] <= {gpio_width{1'b0}};
231
+
232
+ gpio_itr_status_regs[0] <= 1'b0;
233
+ gpio_itr_en_regs[gpio_width-1:0] <= {gpio_width{1'b0}};
234
+ end
235
+ else if(psel & pwrite & penable)
236
+ begin
237
+ # simulation_delay;
238
+
239
+ case(paddr[4:2])
240
+ 3'd1: // 写GPIO写电平掩码
241
+ gpio_o_mask_regs[gpio_width-1:0] <= pwdata[gpio_width-1:0];
242
+ 3'd2: // 写GPIO方向, 仅inout下修改有效
243
+ gpio_direction_regs[gpio_width-1:0] <= pwdata[gpio_width-1:0];
244
+ 3'd4: // 写全局中断使能
245
+ gpio_itr_status_regs[0] <= pwdata[0];
246
+ 3'd6: // 写中断使能
247
+ gpio_itr_en_regs[gpio_width-1:0] <= pwdata[gpio_width-1:0];
248
+ default: // hold
249
+ begin
250
+ gpio_o_mask_regs[gpio_width-1:0] <= gpio_o_mask_regs[gpio_width-1:0];
251
+ gpio_direction_regs[gpio_width-1:0] <= gpio_direction_regs[gpio_width-1:0];
252
+ gpio_itr_status_regs[0] <= gpio_itr_status_regs[0];
253
+ gpio_itr_en_regs[gpio_width-1:0] <= gpio_itr_en_regs[gpio_width-1:0];
254
+ end
255
+ endcase
256
+ end
257
+ end
258
+
259
+ always @(posedge clk or negedge resetn)
260
+ begin
261
+ if(~resetn)
262
+ gpio_itr_status_regs[1] <= 1'b0;
263
+ else if(psel & pwrite & penable & (paddr[4:2] == 3'd4)) // 清除中断���志
264
+ # simulation_delay gpio_itr_status_regs[1] <= 1'b0;
265
+ else if(~gpio_itr_status_regs[1]) // 等待新的GPIO中断, 中断标志为高时屏蔽新的GPIO中断
266
+ # simulation_delay gpio_itr_status_regs[1] <= gpio_org_itr_pulse;
267
+ end
268
+
269
+ // 生成状态信息
270
+ always @(posedge clk or negedge resetn)
271
+ begin
272
+ if(~resetn)
273
+ begin
274
+ gpio_i_value_regs[gpio_width-1:0] <= (itr_edge == "pos") ? {gpio_width{1'b0}}:{gpio_width{1'b1}};
275
+ gpio_itr_mask_regs[gpio_width-1:0] <= {gpio_width{1'b0}};
276
+ end
277
+ else
278
+ begin
279
+ # simulation_delay;
280
+
281
+ gpio_i_value_regs[gpio_width-1:0] <= gpio_i_w; // 载入GPIO输入电平
282
+
283
+ if(gpio_org_itr_pulse) // 载入中断状态
284
+ gpio_itr_mask_regs[gpio_width-1:0] <= gpio_itr_mask_w_d;
285
+ end
286
+ end
287
+
288
+ /** APB读寄存器 **/
289
+ reg[31:0] prdata_out_regs;
290
+
291
+ assign pready_out = 1'b1;
292
+ assign pslverr_out = 1'b0;
293
+ assign prdata_out = prdata_out_regs;
294
+
295
+ always @(posedge clk)
296
+ begin
297
+ if(psel & (~pwrite))
298
+ begin
299
+ # simulation_delay;
300
+
301
+ case(paddr[4:2])
302
+ 3'd3: // 读GPIO输入
303
+ prdata_out_regs <= {{(32-gpio_width){1'bx}}, gpio_i_value_regs[gpio_width-1:0]};
304
+ 3'd4: // 读中断标志
305
+ prdata_out_regs <= {30'dx, gpio_itr_status_regs[1], 1'bx};
306
+ 3'd5: // 读中断状态
307
+ prdata_out_regs <= {{(32-gpio_width){1'bx}}, gpio_itr_mask_regs[gpio_width-1:0]};
308
+ default: // not care
309
+ prdata_out_regs <= 32'dx;
310
+ endcase
311
+ end
312
+ end
313
+
314
+ endmodule
CutestPanda_Opensoc/100_apb_gpio/rtl/itr_generator.v ADDED
@@ -0,0 +1,104 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ MIT License
3
+
4
+ Copyright (c) 2024 Panda, 2257691535@qq.com
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
23
+ */
24
+
25
+ `timescale 1ns / 1ps
26
+ /********************************************************************
27
+ 本模块: 中断信号发生器
28
+
29
+ 描述:
30
+ 将原始的中断脉冲拓展为一定脉宽的中断信号
31
+
32
+ 注意:
33
+ 中断脉宽必须 >= 2
34
+
35
+ 协议:
36
+
37
+
38
+ 作者: 陈家耀
39
+ 日期: 2023/11/08
40
+ ********************************************************************/
41
+
42
+
43
+ module itr_generator #(
44
+ parameter integer pulse_w = 100, // 中断脉宽(必须>=1)
45
+ parameter real simulation_delay = 1 // 仿真延时
46
+ )(
47
+ // 时钟和复位
48
+ input wire clk,
49
+ input wire rst_n,
50
+
51
+ input wire itr_org, // 原始中断输入
52
+ output wire itr // 中断输出
53
+ );
54
+
55
+ // 计算log2(bit_depth)
56
+ function integer clogb2 (input integer bit_depth);
57
+ integer temp;
58
+ begin
59
+ temp = bit_depth;
60
+ for(clogb2 = -1;temp > 0;clogb2 = clogb2 + 1)
61
+ temp = temp >> 1;
62
+ end
63
+ endfunction
64
+
65
+ // 产生中断信号
66
+ reg itr_reg; // 中断信号
67
+ reg[clogb2(pulse_w - 1):0] itr_cnt; // 中断计数器
68
+
69
+ assign itr = itr_reg;
70
+
71
+ generate
72
+ if(pulse_w == 1)
73
+ begin
74
+ always @(posedge clk or negedge rst_n)
75
+ begin
76
+ if(~rst_n)
77
+ itr_reg <= 1'b0;
78
+ else
79
+ # simulation_delay itr_reg <= itr_org;
80
+ end
81
+ end
82
+ else
83
+ begin
84
+ always @(posedge clk or negedge rst_n)
85
+ begin
86
+ if(~rst_n)
87
+ itr_reg <= 1'b0;
88
+ else
89
+ begin
90
+ # simulation_delay;
91
+
92
+ if(~itr_reg) // 等待原始中断脉冲
93
+ itr_reg <= itr_org;
94
+ else // 等待计数完成
95
+ itr_reg <= itr_cnt != pulse_w - 1;
96
+ end
97
+ end
98
+ end
99
+ endgenerate
100
+
101
+ always @(posedge clk)
102
+ # simulation_delay itr_cnt <= (~itr_reg) ? 0:(itr_cnt + 1);
103
+
104
+ endmodule
CutestPanda_Opensoc/101_apb_uart/firmware/apb_uart.c ADDED
@@ -0,0 +1,153 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /************************************************************************************************************************
2
+ APB-UART����(��Դ�ļ�)
3
+ @brief APB-UART����
4
+ @date 2024/08/28
5
+ @author �¼�ҫ
6
+ @eidt 2024/08/28 1.00 �����˵�һ����ʽ�汾
7
+ ************************************************************************************************************************/
8
+
9
+ #include "apb_uart.h"
10
+
11
+ ////////////////////////////////////////////////////////////////////////////////////////////////////////////
12
+
13
+ /*************************
14
+ @init
15
+ @public
16
+ @brief ��ʼ��APB-UART
17
+ @param uart APB-UART(�ṹ��ָ��)
18
+ base_addr APB-UART�������ַ
19
+ @return none
20
+ *************************/
21
+ void apb_uart_init(ApbUART* uart, uint32_t base_addr){
22
+ uart->hardware = (ApbUARTHd*)base_addr;
23
+ uart->itr_en = 0x00;
24
+ }
25
+
26
+ /*************************
27
+ @io
28
+ @public
29
+ @brief APB-UART����һ���ֽ�
30
+ @param uart APB-UART(�ṹ��ָ��)
31
+ byte �����͵��ֽ�����
32
+ @return �Ƿ�ɹ�
33
+ *************************/
34
+ int apb_uart_send_byte(ApbUART* uart, uint8_t byte){
35
+ uint32_t fifo_cs = uart->hardware->fifo_cs;
36
+
37
+ if(fifo_cs & 0x01){
38
+ return -1;
39
+ }else{
40
+ fifo_cs &= (~0x3FC);
41
+ fifo_cs |= ((uint32_t)byte) << 2;
42
+ uart->hardware->fifo_cs = fifo_cs;
43
+
44
+ uart->hardware->fifo_cs = (fifo_cs | 0x02);
45
+ uart->hardware->fifo_cs = (fifo_cs & (~0x02));
46
+
47
+ return 0;
48
+ }
49
+ }
50
+
51
+ /*************************
52
+ @io
53
+ @public
54
+ @brief APB-UART��ȡһ�������ֽ�
55
+ @param uart APB-UART(�ṹ��ָ��)
56
+ byte �����ֽ����ݻ�����(�׵�ַ)
57
+ @return �Ƿ�ɹ�
58
+ *************************/
59
+ int apb_uart_rev_byte(ApbUART* uart, uint8_t* byte){
60
+ uint32_t fifo_cs = uart->hardware->fifo_cs;
61
+
62
+ if(fifo_cs & 0x400){
63
+ return -1;
64
+ }else{
65
+ uart->hardware->fifo_cs = (fifo_cs | 0x800);
66
+ uart->hardware->fifo_cs = (fifo_cs & (~0x800));
67
+
68
+ fifo_cs = uart->hardware->fifo_cs;
69
+ *byte = (fifo_cs >> 12) & 0xFF;
70
+
71
+ return 0;
72
+ }
73
+ }
74
+
75
+ /*************************
76
+ @cfg
77
+ @public
78
+ @brief APB-UARTʹ���ж�
79
+ @param uart APB-UART(�ṹ��ָ��)
80
+ itr_mask �ж�ʹ������
81
+ config �շ��ж���ֵ����(�ṹ��ָ��)
82
+ @return none
83
+ *************************/
84
+ void apb_uart_enable_itr(ApbUART* uart, uint8_t itr_mask, const ApbUartItrThConfig* config){
85
+ uart->hardware->itr_status_en = 0x00000001 | (itr_mask << 1);
86
+ uart->itr_en = itr_mask;
87
+
88
+ uart->hardware->tx_itr_th =
89
+ ((uint32_t)(config->tx_bytes_n_th - 1)) | (((uint32_t)(config->tx_idle_th - 2)) << 16);
90
+ uart->hardware->rx_itr_th =
91
+ ((uint32_t)(config->rx_bytes_n_th - 1)) | (((uint32_t)(config->rx_idle_th - 2)) << 16);
92
+ }
93
+
94
+ /*************************
95
+ @cfg
96
+ @public
97
+ @brief APB-UART�����ж�
98
+ @param uart APB-UART(�ṹ��ָ��)
99
+ @return none
100
+ *************************/
101
+ void apb_uart_disable_itr(ApbUART* uart){
102
+ uart->hardware->itr_status_en = 0x00000000;
103
+ uart->itr_en = 0x00;
104
+ }
105
+
106
+ /*************************
107
+ @sts
108
+ @public
109
+ @brief APB-UART��ȡ�ж�״̬
110
+ @param uart APB-UART(�ṹ��ָ��)
111
+ @return �ж�״̬
112
+ *************************/
113
+ uint8_t apb_uart_get_itr_status(ApbUART* uart){
114
+ uint32_t itr_status_en = uart->hardware->itr_status_en;
115
+
116
+ return (uint8_t)(itr_status_en >> 17) & 0x1F;
117
+ }
118
+
119
+ /*************************
120
+ @cfg
121
+ @public
122
+ @brief APB-UART����жϱ�־
123
+ @param uart APB-UART(�ṹ��ָ��)
124
+ @return none
125
+ *************************/
126
+ void apb_uart_clear_itr_flag(ApbUART* uart){
127
+ uart->hardware->itr_status_en = 0x00000001 | (uart->itr_en << 1);
128
+ }
129
+
130
+ /*************************
131
+ @io
132
+ @public
133
+ @brief APB-UART��ʽ�������ַ���
134
+ @param uart APB-UART(�ṹ��ָ��)
135
+ fmt ��ʽ���ַ���
136
+ ... �ַ������Ӳ���
137
+ @return none
138
+ *************************/
139
+ void uart_printf(ApbUART* uart, char *fmt, ...){
140
+ unsigned char UsartPrintfBuf[296];
141
+ va_list ap;
142
+ unsigned char* pStr = UsartPrintfBuf;
143
+
144
+ va_start(ap, fmt);
145
+ vsnprintf((char *)UsartPrintfBuf, sizeof(UsartPrintfBuf), (const char *)fmt, ap);
146
+ va_end(ap);
147
+
148
+ while(*pStr != 0){
149
+ while (!apb_uart_send_byte(uart, *pStr));
150
+
151
+ pStr++;
152
+ }
153
+ }
CutestPanda_Opensoc/101_apb_uart/firmware/apb_uart.h ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /************************************************************************************************************************
2
+ APB-UART����(�ӿ�ͷ�ļ�)
3
+ @brief APB-UART����
4
+ @date 2024/08/28
5
+ @author �¼�ҫ
6
+ ************************************************************************************************************************/
7
+
8
+ #include <stdint.h>
9
+ #include <stdarg.h>
10
+ #include <string.h>
11
+ #include <stdio.h>
12
+
13
+ ////////////////////////////////////////////////////////////////////////////////////////////////////////////
14
+
15
+ #ifndef __APB_UART_H
16
+ #define __APB_UART_H
17
+
18
+ typedef struct{
19
+ uint32_t fifo_cs; // �շ�fifo����
20
+ uint32_t itr_status_en; // �жϿ���
21
+ uint32_t tx_itr_th; // �����ж���ֵ
22
+ uint32_t rx_itr_th; // �����ж���ֵ
23
+ }ApbUARTHd;
24
+
25
+ typedef struct{
26
+ ApbUARTHd* hardware; // APB-UART�Ĵ����ӿ�(�ṹ��ָ��)
27
+ uint8_t itr_en; // ��ǰ���ж�ʹ������
28
+ }ApbUART;
29
+
30
+ typedef struct{
31
+ uint16_t tx_bytes_n_th; // UART�����ж��ֽ�����ֵ
32
+ uint16_t tx_idle_th; // UART�����ж�IDLE��������ֵ
33
+ uint16_t rx_bytes_n_th; // UART�����ж��ֽ�����ֵ
34
+ uint16_t rx_idle_th; // UART�����ж�IDLE��������ֵ
35
+ }ApbUartItrThConfig;
36
+
37
+ #endif
38
+
39
+ ////////////////////////////////////////////////////////////////////////////////////////////////////////////
40
+
41
+ // ���������
42
+ #define APB_UART_TX_BYTES_N_ITR_MASK 0x01 // UART���ʹﵽ�涨�ֽ����ж�
43
+ #define APB_UART_TX_IDLE_ITR_MASK 0x02 // UART����IDLE�ж�
44
+ #define APB_UART_RX_BYTES_N_ITR_MASK 0x04 // UART���մﵽ�涨�ֽ����ж�
45
+ #define APB_UART_RX_IDLE_ITR_MASK 0x08 // UART����IDLE�ж�
46
+ #define APB_UART_RX_ERR_ITR_MASK 0x10 // UART����FIFO����ж�
47
+
48
+ ////////////////////////////////////////////////////////////////////////////////////////////////////////////
49
+
50
+ void apb_uart_init(ApbUART* uart, uint32_t base_addr); // ��ʼ��APB-UART
51
+
52
+ int apb_uart_send_byte(ApbUART* uart, uint8_t byte); // APB-UART����һ���ֽ�
53
+ int apb_uart_rev_byte(ApbUART* uart, uint8_t* byte); // APB-UART��ȡһ�������ֽ�
54
+
55
+ void apb_uart_enable_itr(ApbUART* uart, uint8_t itr_mask, const ApbUartItrThConfig* config); // APB-UARTʹ���ж�
56
+ void apb_uart_disable_itr(ApbUART* uart); // APB-UART�����ж�
57
+ uint8_t apb_uart_get_itr_status(ApbUART* uart); // APB-UART��ȡ�ж�״̬
58
+ void apb_uart_clear_itr_flag(ApbUART* uart); // APB-UART����жϱ�־
59
+
60
+ void uart_printf(ApbUART* uart, char *fmt, ...); // APB-UART��ʽ�������ַ���
CutestPanda_Opensoc/101_apb_uart/firmware/examples/apb_uart_rx_itr_example.c ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /************************************************************************************************************************
2
+ APB-UARTʾ������
3
+ @brief UART����IDLE�ж�ʾ��
4
+ @attention �����Ӳ��ƽ̨������ȫ���жϿ�������ص�API
5
+ @date 2024/08/28
6
+ @author �¼�ҫ
7
+ @eidt 2024/08/28 1.00 �����˵�һ����ʽ�汾
8
+ ************************************************************************************************************************/
9
+
10
+ #include "../apb_uart.h"
11
+
12
+ #include "CMSDK_CM0.h"
13
+
14
+ ////////////////////////////////////////////////////////////////////////////////////////////////////////////
15
+
16
+ #define APB_UART_BASEADDR 0x40000000 // APB-UART�������ַ
17
+
18
+ ////////////////////////////////////////////////////////////////////////////////////////////////////////////
19
+
20
+ static ApbUART uart; // APB-UART����ṹ��
21
+
22
+ ////////////////////////////////////////////////////////////////////////////////////////////////////////////
23
+
24
+ /*************************
25
+ @itr_handler
26
+ @private
27
+ @brief APB-UART�շ��жϷ������(ʾ��)
28
+ @param none
29
+ @return none
30
+ *************************/
31
+ void USER_UART_Handler(void){
32
+ uint8_t byte;
33
+
34
+ while(!apb_uart_rev_byte(&uart, &byte)){ // �ռ�UART���ݰ�
35
+ apb_uart_send_byte(&uart, byte); // ��ԭ������
36
+ }
37
+
38
+ apb_uart_clear_itr_flag(&uart); // �����жϱ�־
39
+ }
40
+
41
+ ////////////////////////////////////////////////////////////////////////////////////////////////////////////
42
+
43
+ void apb_uart_rx_itr_example(void){
44
+ apb_uart_init(&uart, APB_UART_BASEADDR); // ��ʼ��APB-UART
45
+
46
+ // ����UART����IDLE�ж�
47
+ const ApbUartItrThConfig uart_config = {10, 100, 10, 100}; // UART�����ж�IDLE��������ֵ = 100
48
+
49
+ apb_uart_enable_itr(&uart, APB_UART_RX_IDLE_ITR_MASK, &uart_config); // ʹ��UART����IDLE�ж�
50
+
51
+ NVIC_SetPriority((IRQn_Type)4, 0x03); // NVIC����4���ж����ȼ�
52
+ NVIC_EnableIRQ((IRQn_Type)4); // NVICʹ��4���ж�
53
+
54
+ while(1);
55
+ }
CutestPanda_Opensoc/101_apb_uart/firmware/examples/apb_uart_tx_example.c ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /************************************************************************************************************************
2
+ APB-UARTʾ������
3
+ @brief UART����ʾ��
4
+ @date 2024/08/28
5
+ @author �¼�ҫ
6
+ @eidt 2024/08/28 1.00 �����˵�һ����ʽ�汾
7
+ ************************************************************************************************************************/
8
+
9
+ #include "../apb_uart.h"
10
+
11
+ ////////////////////////////////////////////////////////////////////////////////////////////////////////////
12
+
13
+ #define APB_UART_BASEADDR 0x40000000 // APB-UART�������ַ
14
+
15
+ ////////////////////////////////////////////////////////////////////////////////////////////////////////////
16
+
17
+ static ApbUART uart; // APB-UART����ṹ��
18
+
19
+ ////////////////////////////////////////////////////////////////////////////////////////////////////////////
20
+
21
+ void apb_uart_tx_example(void){
22
+ apb_uart_init(&uart, APB_UART_BASEADDR); // ��ʼ��APB-UART
23
+
24
+ // �����ַ���
25
+ uart_printf(&uart, "hello, world!\n\r");
26
+
27
+ while(1);
28
+ }
CutestPanda_Opensoc/101_apb_uart/rtl/apb_uart.v ADDED
@@ -0,0 +1,617 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ `timescale 1ns / 1ps
2
+ /********************************************************************
3
+ 本模块: 符合APB协议的UART控制器
4
+
5
+ 描述:
6
+ APB-UART控制器
7
+ 支持UART发送/接收中断
8
+
9
+ 寄存器->
10
+ 偏移量 | 含义 | 读写特性 | 备注
11
+ 0x00 0:发送fifo是否满 R
12
+ 1:发送fifo写使能 W 发送fifo写使能取上升沿
13
+ 9~2:发送fifo写数据 W
14
+ 10:接收fifo是否空 R
15
+ 11:接收fifo读使能 W 接收fifo读使能取上升沿
16
+ 19~12:接收fifo读数据 R
17
+ 0x04 0:UART全局中断使能 W
18
+ 1:UART发送达到规定字节数中断使能 W
19
+ 2:UART发送IDLE中断使能 W
20
+ 3:UART接收达到规定字节数中断使能 W
21
+ 4:UART接收IDLE中断使能 W
22
+ 5:UART接收FIFO溢出中断使能 W
23
+ 16:UART中断标志 RWC 请在中断服务函数中清除中断标志
24
+ 21~17:UART中断状态 R
25
+ 0x08 15~0:UART发送中断字节数阈值 W 注意要减1
26
+ 31~16:UART发送中断IDLE周期数阈值 W 注意要减2
27
+ 0x0C 15~0:UART接收中断字节数阈值 W 注意要减1
28
+ 31~16:UART接收中断IDLE周期数阈值 W 注意要减2
29
+
30
+ 注意:
31
+
32
+
33
+ 协议:
34
+ APB SLAVE
35
+ UART
36
+
37
+ 作者: 陈家耀
38
+ 日期: 2023/11/07
39
+ ********************************************************************/
40
+
41
+
42
+ module apb_uart #(
43
+ parameter integer clk_frequency_MHz = 50, // 时钟频率
44
+ parameter integer baud_rate = 115200, // 波特率
45
+ parameter tx_rx_fifo_ram_type = "bram", // 发送接收fifo的RAM类型(lutram|bram)
46
+ parameter integer tx_fifo_depth = 1024, // 发送fifo深度(32|64|128|...|2048)
47
+ parameter integer rx_fifo_depth = 1024, // 接收fifo深度(32|64|128|...|2048)
48
+ parameter en_itr = "false", // 是否使能UART中断
49
+ parameter real simulation_delay = 1 // 仿真延时
50
+ )(
51
+ // 时钟和复位
52
+ input wire clk,
53
+ input wire resetn,
54
+
55
+ // APB从机接口
56
+ input wire[31:0] paddr,
57
+ input wire psel,
58
+ input wire penable,
59
+ input wire pwrite,
60
+ input wire[31:0] pwdata,
61
+ output wire pready_out, // const -> 1'b1
62
+ output wire[31:0] prdata_out,
63
+ output wire pslverr_out, // const -> 1'b0
64
+
65
+ // UART
66
+ output wire uart_tx,
67
+ input wire uart_rx,
68
+
69
+ // 中断
70
+ output wire uart_itr
71
+ );
72
+
73
+ /* UART发送和接收fifo */
74
+ // 发送fifo写端口
75
+ wire tx_fifo_wen;
76
+ wire tx_fifo_full;
77
+ wire[7:0] tx_fifo_din;
78
+ // 发送fifo读端口
79
+ wire tx_fifo_ren;
80
+ wire tx_fifo_empty;
81
+ wire[7:0] tx_fifo_dout;
82
+ // 接收fifo写端口
83
+ wire rx_fifo_wen;
84
+ wire rx_fifo_full;
85
+ wire[7:0] rx_fifo_din;
86
+ // 接收fifo读端口
87
+ wire rx_fifo_ren;
88
+ wire rx_fifo_empty;
89
+ wire[7:0] rx_fifo_dout;
90
+
91
+ // 发送fifo
92
+ ram_fifo_wrapper #(
93
+ .fwft_mode("false"),
94
+ .ram_type(tx_rx_fifo_ram_type),
95
+ .en_bram_reg("false"),
96
+ .fifo_depth(tx_fifo_depth),
97
+ .fifo_data_width(8),
98
+ .full_assert_polarity("high"),
99
+ .empty_assert_polarity("high"),
100
+ .almost_full_assert_polarity("no"),
101
+ .almost_empty_assert_polarity("no"),
102
+ .en_data_cnt("false"),
103
+ .almost_full_th(),
104
+ .almost_empty_th(),
105
+ .simulation_delay(simulation_delay)
106
+ )tx_fifo(
107
+ .clk(clk),
108
+ .rst_n(resetn),
109
+ .fifo_wen(tx_fifo_wen),
110
+ .fifo_din(tx_fifo_din),
111
+ .fifo_full(tx_fifo_full),
112
+ .fifo_ren(tx_fifo_ren),
113
+ .fifo_dout(tx_fifo_dout),
114
+ .fifo_empty(tx_fifo_empty)
115
+ );
116
+
117
+ // 接收fifo
118
+ ram_fifo_wrapper #(
119
+ .fwft_mode("false"),
120
+ .ram_type(tx_rx_fifo_ram_type),
121
+ .en_bram_reg("false"),
122
+ .fifo_depth(rx_fifo_depth),
123
+ .fifo_data_width(8),
124
+ .full_assert_polarity("high"),
125
+ .empty_assert_polarity("high"),
126
+ .almost_full_assert_polarity("no"),
127
+ .almost_empty_assert_polarity("no"),
128
+ .en_data_cnt("false"),
129
+ .almost_full_th(),
130
+ .almost_empty_th(),
131
+ .simulation_delay(simulation_delay)
132
+ )rx_fifo(
133
+ .clk(clk),
134
+ .rst_n(resetn),
135
+ .fifo_wen(rx_fifo_wen),
136
+ .fifo_din(rx_fifo_din),
137
+ .fifo_full(rx_fifo_full),
138
+ .fifo_ren(rx_fifo_ren),
139
+ .fifo_dout(rx_fifo_dout),
140
+ .fifo_empty(rx_fifo_empty)
141
+ );
142
+
143
+ /** UART中断 **/
144
+ wire uart_org_itr_pulse; // 原始的UART中断脉冲
145
+ wire uart_org_itr_pulse_d; // 延迟1clk的原始的UART中断脉冲
146
+ wire uart_itr_flag; // UART总中断标志
147
+ wire[4:0] uart_itr_mask; // UART子中断标志向量(UART接收FIFO溢出中断使能, UART接收IDLE中断使能, UART接收达到规定字节数中断使能, UART发送IDLE中断使能, UART发送达到规定字节数中断使能)
148
+ wire[4:0] uart_itr_mask_d; // 延迟1clk的UART子中断标志向量
149
+ wire uart_global_itr_en; // UART全局中断使能
150
+ wire[4:0] uart_itr_en; // UART中断使能(UART接收FIFO溢出中断使能, UART接收IDLE中断使能, UART接收达到规定字节数中断使能, UART发送IDLE中断使能, UART发送达到规定字节数中断使能)
151
+
152
+ wire[15:0] tx_bytes_n_th; // UART发送中断字节数阈值
153
+ wire[15:0] tx_bytes_n_th_sub1; // UART发送中断字节数阈值-1
154
+ wire tx_bytes_n_th_eq0; // UART发送中断字节数阈值等于0(标志)
155
+ wire[15:0] tx_idle_n_th; // UART发送中断IDLE周期数阈值
156
+ wire[15:0] rx_bytes_n_th; // UART接收中断字节数阈值
157
+ wire[15:0] rx_bytes_n_th_sub1; // UART接收中断字节数阈值-1
158
+ wire rx_bytes_n_th_eq0; // UART接收中断字节数阈值等于0(标志)
159
+ wire[15:0] rx_idle_n_th; // UART接收中断IDLE周期数阈值
160
+
161
+ wire rx_err; // 接收溢出(脉冲)
162
+ wire tx_idle; // 发送空闲(标志)
163
+ wire rx_idle; // 接收空闲(标志)
164
+ wire tx_done; // 发送完成(脉冲)
165
+ wire rx_done; // 接收完成(脉冲)
166
+ wire rx_start; // 接收开始(脉冲)
167
+
168
+ generate
169
+ if(en_itr == "true")
170
+ begin
171
+ reg[15:0] uart_tx_finished_bytes_n_cnt; // 已发送字节数(计数器)
172
+ reg uart_tx_finished_bytes_n_cnt_eq_th_sub1; // 已发送字节数 == UART发送中断字节数阈值-1(标志)
173
+ reg uart_tx_finished_bytes_n_itr_pulse; // UART发送达到规定字节数(原始中断脉冲)
174
+
175
+ always @(posedge clk or negedge resetn)
176
+ begin
177
+ if(~resetn)
178
+ begin
179
+ uart_tx_finished_bytes_n_cnt <= 16'd0;
180
+ uart_tx_finished_bytes_n_cnt_eq_th_sub1 <= 1'b0;
181
+ end
182
+ else if((~uart_itr_en[0]) | (~uart_global_itr_en))
183
+ begin
184
+ # simulation_delay;
185
+
186
+ uart_tx_finished_bytes_n_cnt <= 16'd0;
187
+ uart_tx_finished_bytes_n_cnt_eq_th_sub1 <= 1'b0;
188
+ end
189
+ else if(tx_done)
190
+ begin
191
+ # simulation_delay;
192
+
193
+ uart_tx_finished_bytes_n_cnt <= (uart_tx_finished_bytes_n_cnt_eq_th_sub1 | tx_bytes_n_th_eq0) ? 16'd0:(uart_tx_finished_bytes_n_cnt + 16'd1);
194
+ uart_tx_finished_bytes_n_cnt_eq_th_sub1 <= uart_tx_finished_bytes_n_cnt == tx_bytes_n_th_sub1;
195
+ end
196
+ end
197
+
198
+ always @(posedge clk or negedge resetn)
199
+ begin
200
+ if(~resetn)
201
+ uart_tx_finished_bytes_n_itr_pulse <= 1'b0;
202
+ else
203
+ # simulation_delay uart_tx_finished_bytes_n_itr_pulse <= (uart_itr_en[0] & uart_global_itr_en) &
204
+ (uart_tx_finished_bytes_n_cnt_eq_th_sub1 | tx_bytes_n_th_eq0) & tx_done;
205
+ end
206
+
207
+ reg is_waiting_for_uart_tx_done; // 是否正在等待UART发送完成脉冲
208
+ reg[15:0] uart_tx_idle_cnt; // UART发送空闲周期数(计数器)
209
+ reg uart_tx_idle_cnt_eq_th_sub1; // UART发送空闲周期数 == UART发送中断IDLE周期数阈值
210
+ reg uart_tx_idle_itr_pulse; // UART发送空闲(原始中断脉冲)
211
+
212
+ always @(posedge clk or negedge resetn)
213
+ begin
214
+ if(~resetn)
215
+ is_waiting_for_uart_tx_done <= 1'b1;
216
+ else
217
+ # simulation_delay is_waiting_for_uart_tx_done <= is_waiting_for_uart_tx_done ? (~tx_done):(uart_tx_idle_cnt_eq_th_sub1 | (~tx_idle));
218
+ end
219
+
220
+ always @(posedge clk or negedge resetn)
221
+ begin
222
+ if(~resetn)
223
+ begin
224
+ uart_tx_idle_cnt <= 16'd0;
225
+ uart_tx_idle_cnt_eq_th_sub1 <= 1'b0;
226
+ end
227
+ else if((~uart_itr_en[1]) | (~uart_global_itr_en))
228
+ begin
229
+ # simulation_delay;
230
+
231
+ uart_tx_idle_cnt <= 16'd0;
232
+ uart_tx_idle_cnt_eq_th_sub1 <= 1'b0;
233
+ end
234
+ else if(~is_waiting_for_uart_tx_done)
235
+ begin
236
+ # simulation_delay;
237
+
238
+ uart_tx_idle_cnt <= (uart_tx_idle_cnt_eq_th_sub1 | (~tx_idle)) ? 16'd0:(uart_tx_idle_cnt + 16'd1);
239
+ uart_tx_idle_cnt_eq_th_sub1 <= uart_tx_idle_cnt == tx_idle_n_th;
240
+ end
241
+ end
242
+
243
+ always @(posedge clk or negedge resetn)
244
+ begin
245
+ if(~resetn)
246
+ uart_tx_idle_itr_pulse <= 1'b0;
247
+ else
248
+ # simulation_delay uart_tx_idle_itr_pulse <= (~is_waiting_for_uart_tx_done) & uart_tx_idle_cnt_eq_th_sub1 & tx_idle;
249
+ end
250
+
251
+ reg[15:0] uart_rx_finished_bytes_n_cnt;
252
+ reg uart_rx_finished_bytes_n_cnt_eq_th_sub1;
253
+ reg uart_rx_finished_bytes_n_itr_pulse;
254
+
255
+ always @(posedge clk or negedge resetn)
256
+ begin
257
+ if(~resetn)
258
+ begin
259
+ uart_rx_finished_bytes_n_cnt <= 16'd0;
260
+ uart_rx_finished_bytes_n_cnt_eq_th_sub1 <= 1'b0;
261
+ end
262
+ else if((~uart_itr_en[2]) | (~uart_global_itr_en))
263
+ begin
264
+ # simulation_delay;
265
+
266
+ uart_rx_finished_bytes_n_cnt <= 16'd0;
267
+ uart_rx_finished_bytes_n_cnt_eq_th_sub1 <= 1'b0;
268
+ end
269
+ else if(rx_done)
270
+ begin
271
+ # simulation_delay;
272
+
273
+ uart_rx_finished_bytes_n_cnt <= (uart_rx_finished_bytes_n_cnt_eq_th_sub1 | rx_bytes_n_th_eq0) ? 16'd0:(uart_rx_finished_bytes_n_cnt + 16'd1);
274
+ uart_rx_finished_bytes_n_cnt_eq_th_sub1 <= uart_rx_finished_bytes_n_cnt == rx_bytes_n_th_sub1;
275
+ end
276
+ end
277
+
278
+ always @(posedge clk or negedge resetn)
279
+ begin
280
+ if(~resetn)
281
+ uart_rx_finished_bytes_n_itr_pulse <= 1'b0;
282
+ else
283
+ # simulation_delay uart_rx_finished_bytes_n_itr_pulse <= (uart_itr_en[2] & uart_global_itr_en) &
284
+ (uart_rx_finished_bytes_n_cnt_eq_th_sub1 | rx_bytes_n_th_eq0) & rx_done;
285
+ end
286
+
287
+ reg is_waiting_for_uart_rx_done;
288
+ reg[15:0] uart_rx_idle_cnt;
289
+ reg uart_rx_idle_cnt_eq_th_sub1;
290
+ reg uart_rx_idle_itr_pulse;
291
+
292
+ always @(posedge clk or negedge resetn)
293
+ begin
294
+ if(~resetn)
295
+ is_waiting_for_uart_rx_done <= 1'b1;
296
+ else
297
+ # simulation_delay is_waiting_for_uart_rx_done <= is_waiting_for_uart_rx_done ? (~rx_done):(uart_rx_idle_cnt_eq_th_sub1 | rx_start);
298
+ end
299
+
300
+ always @(posedge clk or negedge resetn)
301
+ begin
302
+ if(~resetn)
303
+ begin
304
+ uart_rx_idle_cnt <= 16'd0;
305
+ uart_rx_idle_cnt_eq_th_sub1 <= 1'b0;
306
+ end
307
+ else if((~uart_itr_en[3]) | (~uart_global_itr_en))
308
+ begin
309
+ # simulation_delay;
310
+
311
+ uart_rx_idle_cnt <= 16'd0;
312
+ uart_rx_idle_cnt_eq_th_sub1 <= 1'b0;
313
+ end
314
+ else if((~is_waiting_for_uart_rx_done) & (rx_start | rx_idle))
315
+ begin
316
+ # simulation_delay;
317
+
318
+ uart_rx_idle_cnt <= (uart_rx_idle_cnt_eq_th_sub1 | rx_start) ? 16'd0:(uart_rx_idle_cnt + 16'd1);
319
+ uart_rx_idle_cnt_eq_th_sub1 <= rx_start ? 1'b0:(uart_rx_idle_cnt == rx_idle_n_th);
320
+ end
321
+ end
322
+
323
+ always @(posedge clk or negedge resetn)
324
+ begin
325
+ if(~resetn)
326
+ uart_rx_idle_itr_pulse <= 1'b0;
327
+ else
328
+ # simulation_delay uart_rx_idle_itr_pulse <= (~is_waiting_for_uart_rx_done) & uart_rx_idle_cnt_eq_th_sub1 & rx_idle;
329
+ end
330
+
331
+ reg rx_err_d;
332
+ reg uart_rx_err_itr_pulse;
333
+
334
+ always @(posedge clk or negedge resetn)
335
+ begin
336
+ if(~resetn)
337
+ rx_err_d <= 1'b0;
338
+ else
339
+ # simulation_delay rx_err_d <= rx_err;
340
+ end
341
+
342
+ always @(posedge clk or negedge resetn)
343
+ begin
344
+ if(~resetn)
345
+ uart_rx_err_itr_pulse <= 1'b0;
346
+ else if((~uart_itr_en[4]) | (~uart_global_itr_en))
347
+ uart_rx_err_itr_pulse <= 1'b0;
348
+ else
349
+ # simulation_delay uart_rx_err_itr_pulse <= rx_err & (~rx_err_d);
350
+ end
351
+
352
+ assign uart_org_itr_pulse = (uart_rx_err_itr_pulse | uart_rx_idle_itr_pulse |
353
+ uart_rx_finished_bytes_n_itr_pulse | uart_tx_idle_itr_pulse | uart_tx_finished_bytes_n_itr_pulse)
354
+ & (~uart_itr_flag);
355
+ assign uart_itr_mask = {uart_rx_err_itr_pulse, uart_rx_idle_itr_pulse, uart_rx_finished_bytes_n_itr_pulse,
356
+ uart_tx_idle_itr_pulse, uart_tx_finished_bytes_n_itr_pulse};
357
+
358
+ reg uart_org_itr_pulse_delay;
359
+ reg[4:0] uart_itr_mask_delay;
360
+
361
+ assign {uart_org_itr_pulse_d, uart_itr_mask_d} = {uart_org_itr_pulse_delay, uart_itr_mask_delay};
362
+
363
+ always @(posedge clk or negedge resetn)
364
+ begin
365
+ if(~resetn)
366
+ begin
367
+ uart_org_itr_pulse_delay <= 1'b0;
368
+ uart_itr_mask_delay <= 5'd0;
369
+ end
370
+ else
371
+ begin
372
+ # simulation_delay;
373
+
374
+ uart_org_itr_pulse_delay <= uart_org_itr_pulse;
375
+ uart_itr_mask_delay <= uart_itr_mask;
376
+ end
377
+ end
378
+
379
+ itr_generator #(
380
+ .pulse_w(10),
381
+ .simulation_delay(simulation_delay)
382
+ )itr_generator_u(
383
+ .clk(clk),
384
+ .rst_n(resetn),
385
+ .itr_org(uart_org_itr_pulse),
386
+ .itr(uart_itr)
387
+ );
388
+ end
389
+ else
390
+ begin
391
+ assign uart_itr = 1'b0;
392
+
393
+ assign {uart_org_itr_pulse, uart_itr_mask} = 6'd0;
394
+ assign {uart_org_itr_pulse_d, uart_itr_mask_d} = 6'd0;
395
+ end
396
+ endgenerate
397
+
398
+ /** UART控制器 **/
399
+ uart_rx_tx #(
400
+ .clk_frequency_MHz(clk_frequency_MHz),
401
+ .baud_rate(baud_rate),
402
+ .interface("fifo"),
403
+ .simulation_delay(simulation_delay)
404
+ )uart_ctrler(
405
+ .clk(clk),
406
+ .resetn(resetn),
407
+ .rx(uart_rx),
408
+ .tx(uart_tx),
409
+ .m_axis_rx_byte_data(),
410
+ .m_axis_rx_byte_valid(),
411
+ .m_axis_rx_byte_ready(),
412
+ .rx_buf_fifo_din(rx_fifo_din),
413
+ .rx_buf_fifo_wen(rx_fifo_wen),
414
+ .rx_buf_fifo_full(rx_fifo_full),
415
+ .s_axis_tx_byte_data(),
416
+ .s_axis_tx_byte_valid(),
417
+ .s_axis_tx_byte_ready(),
418
+ .tx_buf_fifo_dout(tx_fifo_dout),
419
+ .tx_buf_fifo_empty(tx_fifo_empty),
420
+ .tx_buf_fifo_almost_empty(),
421
+ .tx_buf_fifo_ren(tx_fifo_ren),
422
+ .rx_err(rx_err),
423
+ .tx_idle(tx_idle),
424
+ .rx_idle(rx_idle),
425
+ .tx_done(tx_done),
426
+ .rx_done(rx_done),
427
+ .rx_start(rx_start)
428
+ );
429
+
430
+ /*
431
+ 寄存器区->
432
+ 偏移量 | 含义 | 读写特性 | 备注
433
+ 0x00 0:发送fifo是否满 R
434
+ 1:发送fifo写使能 W 发送fifo写使能取上升沿
435
+ 9~2:发送fifo写数据 W
436
+ 10:接收fifo是否空 R
437
+ 11:接收fifo读使能 W 接收fifo读使能取上升沿
438
+ 19~12:接收fifo读数据 R
439
+ 0x04 0:UART全局中断使能 W
440
+ 1:UART发送达到规定字节数中断使能 W
441
+ 2:UART发送IDLE中断使能 W
442
+ 3:UART接收达到规定字节数中断使能 W
443
+ 4:UART接收IDLE中断使能 W
444
+ 5:UART接收FIFO溢出中断使能 W
445
+ 16:UART中断标志 RWC 请在中断服务函数中清除中断标志
446
+ 21~17:UART中断状态 R
447
+ 0x08 15~0:UART发送中断字节数阈值 W 注意要减1
448
+ 31~16:UART发送中断IDLE周期数阈值 W 注意要减2
449
+ 0x0A 15~0:UART接收中断字节数阈值 W 注意要减1
450
+ 31~16:UART接收中断IDLE周期数阈值 W 注意要减2
451
+ */
452
+ // 控制/状态寄存器
453
+ reg[31:0] uart_fifo_cs;
454
+ reg[31:0] uart_itr_status_en;
455
+ reg[31:0] uart_tx_itr_th;
456
+ reg[31:0] uart_rx_itr_th;
457
+
458
+ reg[15:0] tx_bytes_n_th_sub1_regs; // UART发送中断字节数阈值-1
459
+ reg[15:0] rx_bytes_n_th_sub1_regs; // UART接收中断字节数阈值-1
460
+ reg tx_bytes_n_th_eq0_reg; // UART发送中断字节数阈值等于0(标志)
461
+ reg rx_bytes_n_th_eq0_reg; // UART接收中断字节数阈值等于0(标志)
462
+
463
+ reg tx_fifo_wen_d;
464
+ reg rx_fifo_ren_d;
465
+
466
+ assign tx_fifo_wen = uart_fifo_cs[1] & (~tx_fifo_wen_d); // 发送fifo写使能取上升沿
467
+ assign tx_fifo_din = uart_fifo_cs[9:2];
468
+ assign rx_fifo_ren = uart_fifo_cs[11] & (~rx_fifo_ren_d); // 接收fifo读使能取上升沿
469
+
470
+ assign {uart_itr_en, uart_global_itr_en} = uart_itr_status_en[5:0];
471
+ assign uart_itr_flag = uart_itr_status_en[16];
472
+ assign {tx_idle_n_th, tx_bytes_n_th} = uart_tx_itr_th;
473
+ assign {rx_idle_n_th, rx_bytes_n_th} = uart_rx_itr_th;
474
+
475
+ assign {tx_bytes_n_th_sub1, rx_bytes_n_th_sub1} = {tx_bytes_n_th_sub1_regs, rx_bytes_n_th_sub1_regs};
476
+ assign {tx_bytes_n_th_eq0, rx_bytes_n_th_eq0} = {tx_bytes_n_th_eq0_reg, rx_bytes_n_th_eq0_reg};
477
+
478
+ always @(posedge clk or negedge resetn)
479
+ begin
480
+ if(~resetn)
481
+ {tx_fifo_wen_d, rx_fifo_ren_d} <= 2'b00;
482
+ else
483
+ # simulation_delay {tx_fifo_wen_d, rx_fifo_ren_d} <= {uart_fifo_cs[1], uart_fifo_cs[11]};
484
+ end
485
+
486
+ // APB写寄存器
487
+ always @(posedge clk or negedge resetn)
488
+ begin
489
+ if(~resetn)
490
+ begin
491
+ {uart_fifo_cs[11], uart_fifo_cs[9:1]} <= {1'b0, 9'd0};
492
+ {uart_itr_status_en[16], uart_itr_status_en[5:0]} <= {1'b0, 6'd0};
493
+ uart_tx_itr_th <= {16'd100, 16'd1};
494
+ uart_rx_itr_th <= {16'd100, 16'd1};
495
+ end
496
+ else if(psel & pwrite & penable)
497
+ begin
498
+ # simulation_delay;
499
+
500
+ case(paddr[3:2])
501
+ 2'd0: {uart_fifo_cs[11], uart_fifo_cs[9:1]} <= {pwdata[11], pwdata[9:1]}; // 接收fifo读使能, 发送fifo写数据, 发送fifo写使能
502
+ 2'd1: {uart_itr_status_en[16], uart_itr_status_en[5:0]} <= {1'b0, pwdata[5:0]}; // UART总中断标志, UART中断使能
503
+ 2'd2: uart_tx_itr_th <= pwdata; // UART发送中断IDLE周期数阈值, UART发送中断字节数阈值
504
+ 2'd3: uart_rx_itr_th <= pwdata; // UART接收中断IDLE周期数阈值, UART接收中断字节数阈值
505
+ endcase
506
+ end
507
+ else
508
+ begin
509
+ # simulation_delay;
510
+
511
+ if(~uart_itr_status_en[16])
512
+ uart_itr_status_en[16] <= uart_org_itr_pulse; // UART总中断标志
513
+ end
514
+ end
515
+
516
+ // UART发送中断字节数阈值-1
517
+ // UART接收中断字节数阈值-1
518
+ // UART发送中断字节数阈值等于0(标志)
519
+ // UART接收中断字节数阈值等于0(标志)
520
+ always @(posedge clk or negedge resetn)
521
+ begin
522
+ if(~resetn)
523
+ begin
524
+ tx_bytes_n_th_eq0_reg <= 1'b1;
525
+ tx_bytes_n_th_sub1_regs <= 16'hffff;
526
+ rx_bytes_n_th_eq0_reg <= 1'b1;
527
+ rx_bytes_n_th_sub1_regs <= 16'hffff;
528
+ end
529
+ else if(psel & pwrite & penable)
530
+ begin
531
+ # simulation_delay;
532
+
533
+ if(paddr[3:2] == 2)
534
+ begin
535
+ tx_bytes_n_th_eq0_reg <= pwdata[15:0] == 16'd0;
536
+ tx_bytes_n_th_sub1_regs <= pwdata[15:0] - 16'd1;
537
+ end
538
+
539
+ if(paddr[3:2] == 3)
540
+ begin
541
+ rx_bytes_n_th_eq0_reg <= pwdata[15:0] == 16'd0;
542
+ rx_bytes_n_th_sub1_regs <= pwdata[15:0] - 16'd1;
543
+ end
544
+ end
545
+ end
546
+
547
+ // 子中断标志向量
548
+ always @(posedge clk)
549
+ begin
550
+ if(uart_org_itr_pulse_d)
551
+ # simulation_delay uart_itr_status_en[21:17] <= uart_itr_mask_d; // UART子中断标志
552
+ end
553
+
554
+ // APB读寄存器
555
+ reg[31:0] prdata_out_regs;
556
+
557
+ generate
558
+ if(simulation_delay == 0)
559
+ begin
560
+ always @(posedge clk)
561
+ begin
562
+ if(psel & (~pwrite))
563
+ begin
564
+ # simulation_delay;
565
+
566
+ case(paddr[3:2])
567
+ 2'd0:
568
+ begin
569
+ prdata_out_regs[0] <= tx_fifo_full; // 发送fifo是否满
570
+ prdata_out_regs[10] <= rx_fifo_empty; // 接收fifo是否空
571
+ prdata_out_regs[19:12] <= rx_fifo_dout; // 接收fifo读数据
572
+ {prdata_out_regs[31:20], prdata_out_regs[11], prdata_out_regs[9:1]} <= 22'dx;
573
+ end
574
+ 2'd1:
575
+ begin
576
+ prdata_out_regs[21:16] <= uart_itr_status_en[21:16];
577
+ {prdata_out_regs[31:22], prdata_out_regs[15:0]} <= 26'dx;
578
+ end
579
+ default: prdata_out_regs <= 32'dx;
580
+ endcase
581
+ end
582
+ end
583
+ end
584
+ else
585
+ begin
586
+ always @(posedge clk)
587
+ begin
588
+ if(psel & (~pwrite))
589
+ begin
590
+ # simulation_delay;
591
+
592
+ case(paddr[3:2])
593
+ 2'd0:
594
+ begin
595
+ prdata_out_regs[0] <= tx_fifo_full; // 发送fifo是否满
596
+ prdata_out_regs[10] <= rx_fifo_empty; // 接收fifo是否空
597
+ prdata_out_regs[19:12] <= rx_fifo_dout; // 接收fifo读数据
598
+ {prdata_out_regs[31:20], prdata_out_regs[11], prdata_out_regs[9:1]} <= 22'd0;
599
+ end
600
+ 2'd1:
601
+ begin
602
+ prdata_out_regs[21:16] <= uart_itr_status_en[21:16];
603
+ {prdata_out_regs[31:22], prdata_out_regs[15:0]} <= 26'd0;
604
+ end
605
+ default: prdata_out_regs <= 32'd0;
606
+ endcase
607
+ end
608
+ end
609
+ end
610
+ endgenerate
611
+
612
+ /** APB从机接口 **/
613
+ assign pready_out = 1'b1;
614
+ assign prdata_out = prdata_out_regs;
615
+ assign pslverr_out = 1'b0;
616
+
617
+ endmodule
CutestPanda_Opensoc/101_apb_uart/rtl/bram_simple_dual_port.v ADDED
@@ -0,0 +1,132 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ MIT License
3
+
4
+ Copyright (c) 2024 Panda, 2257691535@qq.com
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
23
+ */
24
+
25
+ `timescale 1ns / 1ps
26
+ /********************************************************************
27
+ 本模块: 简单双端口Bram
28
+
29
+ 描述:
30
+ 可选读延迟1clk或2clk
31
+
32
+ 注意:
33
+
34
+
35
+ 协议:
36
+ MEM READ/WRITE
37
+
38
+ 作者: 陈家耀
39
+ 日期: 2022/1/11
40
+ ********************************************************************/
41
+
42
+
43
+ module bram_simple_dual_port #(
44
+ parameter style = "HIGH_PERFORMANCE", // 存储器样式(HIGH_PERFORMANCE|LOW_LATENCY)
45
+ parameter integer mem_width = 32, // 存储器位宽
46
+ parameter integer mem_depth = 4096, // 存储器深度
47
+ parameter INIT_FILE = "no_init", // 初始化文件路径
48
+ parameter integer simulation_delay = 1 // 仿真延时
49
+ )
50
+ (
51
+ // clk
52
+ input wire clk,
53
+
54
+ // mem write
55
+ input wire wen_a,
56
+ input wire[clogb2(mem_depth-1):0] addr_a,
57
+ input wire[mem_width-1:0] din_a,
58
+
59
+ // mem read
60
+ input wire ren_b,
61
+ input wire[clogb2(mem_depth-1):0] addr_b,
62
+ output wire[mem_width-1:0] dout_b
63
+ );
64
+
65
+ // 计算bit_depth的最高有效位编号(即位数-1)
66
+ function integer clogb2 (input integer bit_depth);
67
+ begin
68
+ for(clogb2=-1; bit_depth>0; clogb2=clogb2+1)
69
+ bit_depth = bit_depth >> 1;
70
+ end
71
+ endfunction
72
+
73
+ (* ram_style="block" *) reg[mem_width-1:0] mem[mem_depth-1:0]; // 存储器
74
+ reg[mem_width-1:0] ram_data_b;
75
+
76
+ generate
77
+ if (INIT_FILE != "")
78
+ begin
79
+ if(INIT_FILE == "default")
80
+ begin
81
+ integer ram_index;
82
+ initial
83
+ for (ram_index = 0; ram_index < mem_depth; ram_index = ram_index + 1)
84
+ mem[ram_index] = ram_index;
85
+ end
86
+ else if(INIT_FILE != "no_init")
87
+ begin
88
+ initial
89
+ $readmemh(INIT_FILE, mem, 0, mem_depth - 1);
90
+ end
91
+ end
92
+ else
93
+ begin
94
+ integer ram_index;
95
+ initial
96
+ for (ram_index = 0; ram_index < mem_depth; ram_index = ram_index + 1)
97
+ mem[ram_index] = {mem_width{1'b0}};
98
+ end
99
+ endgenerate
100
+
101
+ // 读写控制逻辑
102
+ always @(posedge clk)
103
+ begin
104
+ if(wen_a)
105
+ # simulation_delay mem[addr_a] <= din_a;
106
+ end
107
+
108
+ always @(posedge clk)
109
+ begin
110
+ if(ren_b)
111
+ # simulation_delay ram_data_b <= mem[addr_b];
112
+ end
113
+
114
+ generate
115
+ if(style == "HIGH_PERFORMANCE")
116
+ begin
117
+ // 使用输出寄存器
118
+ reg[mem_width-1:0] data_b;
119
+
120
+ assign dout_b = data_b;
121
+
122
+ always @(posedge clk)
123
+ #simulation_delay data_b <= ram_data_b;
124
+ end
125
+ else
126
+ begin
127
+ // 不使用输出寄存器
128
+ assign dout_b = ram_data_b;
129
+ end
130
+ endgenerate
131
+
132
+ endmodule
CutestPanda_Opensoc/101_apb_uart/rtl/dram_simple_dual_port.v ADDED
@@ -0,0 +1,129 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ MIT License
3
+
4
+ Copyright (c) 2024 Panda, 2257691535@qq.com
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
23
+ */
24
+
25
+ `timescale 1ns / 1ps
26
+ //////////////////////////////////////////////////////////////////////////////////
27
+ // 项目: 简单双端口Dram
28
+ /*
29
+ 描述:
30
+ 可选的输出寄存器
31
+ */
32
+ /*
33
+ 注意:
34
+
35
+ */
36
+ // 作者: 陈家耀
37
+ // 日期: 2022/1/13
38
+ //////////////////////////////////////////////////////////////////////////////////
39
+
40
+
41
+ module dram_simple_dual_port #(
42
+ parameter integer mem_width = 24, // 存储器位宽
43
+ parameter integer mem_depth = 32, // 存储器深度
44
+ parameter INIT_FILE = "no_init", // 初始化文件路径
45
+ parameter use_output_register = "true", // 是否使用输出寄存器
46
+ parameter output_register_init_v = 0, // 输出寄存器复位值
47
+ parameter real simulation_delay = 10 // 仿真时用的延时
48
+ )(
49
+ // 时钟和复位
50
+ input wire clk,
51
+ input wire rst_n,
52
+
53
+ // mem
54
+ // write port
55
+ input wire wen_a,
56
+ input wire[clogb2(mem_depth-1):0] addr_a,
57
+ input wire[mem_width-1:0] din_a,
58
+ // read port
59
+ input wire ren_b,
60
+ input wire[clogb2(mem_depth-1):0] addr_b,
61
+ output wire[mem_width-1:0] dout_b
62
+ );
63
+
64
+ // 计算bit_depth的最高有效位编号(即位数-1)
65
+ function integer clogb2 (input integer bit_depth);
66
+ integer temp;
67
+ begin
68
+ temp = bit_depth;
69
+ for(clogb2 = -1;temp > 0;clogb2 = clogb2 + 1)
70
+ temp = temp >> 1;
71
+ end
72
+ endfunction
73
+
74
+ (* ram_style="distributed" *) reg[mem_width-1:0] mem[mem_depth-1:0]; // 存储器
75
+
76
+ generate
77
+ if (INIT_FILE != "")
78
+ begin
79
+ if(INIT_FILE != "no_init")
80
+ begin
81
+ initial
82
+ $readmemh(INIT_FILE, mem, 0, mem_depth - 1);
83
+ end
84
+ end
85
+ else
86
+ begin
87
+ integer ram_index;
88
+ initial
89
+ for (ram_index = 0; ram_index < mem_depth; ram_index = ram_index + 1)
90
+ mem[ram_index] = {mem_width{1'b0}};
91
+ end
92
+ endgenerate
93
+
94
+ // 读写控制逻辑
95
+ generate
96
+ if(use_output_register == "true")
97
+ begin
98
+ reg[mem_width-1:0] dout_b_regs;
99
+
100
+ assign dout_b = dout_b_regs;
101
+
102
+ always @(posedge clk)
103
+ begin
104
+ if(wen_a)
105
+ # simulation_delay mem[addr_a] <= din_a;
106
+ end
107
+
108
+ always @(posedge clk or negedge rst_n)
109
+ begin
110
+ if(~rst_n)
111
+ dout_b_regs <= output_register_init_v;
112
+ else if(ren_b)
113
+ # simulation_delay dout_b_regs <= mem[addr_b];
114
+ end
115
+ end
116
+ else
117
+ begin
118
+ always @(posedge clk)
119
+ begin
120
+ # simulation_delay;
121
+ if(wen_a)
122
+ mem[addr_a] <= din_a;
123
+ end
124
+
125
+ assign dout_b = mem[addr_b];
126
+ end
127
+ endgenerate
128
+
129
+ endmodule
CutestPanda_Opensoc/101_apb_uart/rtl/fifo_base_on_ram.v ADDED
@@ -0,0 +1,200 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ MIT License
3
+
4
+ Copyright (c) 2024 Panda, 2257691535@qq.com
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
23
+ */
24
+
25
+ `timescale 1ns / 1ps
26
+ /********************************************************************
27
+ 本模块: 基于ram的同步fifo控制器
28
+
29
+ 描述:
30
+ 全流水的高性能同步fifo控制器
31
+ 基于ram
32
+ 支持first word fall through特性(READ LA = 0)
33
+ 可选的固定阈值将满/将空信号
34
+
35
+ 注意:
36
+ 将满信号当存储计数 >= almost_full_th时有效
37
+ 将空信号当存储计数 <= almost_empty_th时有效
38
+ almost_full_th和almost_empty_th必须在[1, fifo_depth-1]范围内
39
+ 不使能FWFT时, 要求ram的读延迟=1或2clk; 使能FWFT时, 要求ram的读延迟=1clk
40
+
41
+ 协议:
42
+ FIFO WRITE/READ
43
+ MEM WRITE/READ
44
+
45
+ 作者: 陈家耀
46
+ 日期: 2023/10/29
47
+ ********************************************************************/
48
+
49
+
50
+ module fifo_based_on_ram #(
51
+ parameter fwft_mode = "true", // 是否启用first word fall through特性
52
+ parameter ram_read_la = 1, // ram读延迟(1|2)(仅在不使能FWFT时可用)
53
+ parameter integer fifo_depth = 32, // fifo深度(必须为2|4|8|16|...)
54
+ parameter integer fifo_data_width = 32, // fifo位宽
55
+ parameter integer almost_full_th = 20, // fifo将满阈值
56
+ parameter integer almost_empty_th = 5, // fifo将空阈值
57
+ parameter real simulation_delay = 1 // 仿真延时
58
+ )(
59
+ // 时钟和复位
60
+ input wire clk,
61
+ input wire rst_n,
62
+
63
+ // FIFO WRITE(fifo写端口)
64
+ input wire fifo_wen,
65
+ input wire[fifo_data_width-1:0] fifo_din,
66
+ output wire fifo_full,
67
+ output wire fifo_full_n,
68
+ output wire fifo_almost_full,
69
+ output wire fifo_almost_full_n,
70
+
71
+ // FIFO READ(fifo读端口)
72
+ input wire fifo_ren,
73
+ output wire[fifo_data_width-1:0] fifo_dout,
74
+ output wire fifo_empty,
75
+ output wire fifo_empty_n,
76
+ output wire fifo_almost_empty,
77
+ output wire fifo_almost_empty_n,
78
+
79
+ // MEM WRITE(ram写端口)
80
+ output wire ram_wen,
81
+ output wire[clogb2(fifo_depth-1):0] ram_w_addr,
82
+ output wire[fifo_data_width-1:0] ram_din,
83
+
84
+ // MEM RAD(ram读端口)
85
+ output wire ram_ren,
86
+ output wire[clogb2(fifo_depth-1):0] ram_r_addr,
87
+ input wire[fifo_data_width-1:0] ram_dout,
88
+
89
+ // 存储计数
90
+ output wire[clogb2(fifo_depth):0] data_cnt
91
+ );
92
+
93
+ // 计算log2(bit_depth)
94
+ function integer clogb2 (input integer bit_depth);
95
+ integer temp;
96
+ begin
97
+ temp = bit_depth;
98
+ for(clogb2 = -1;temp > 0;clogb2 = clogb2 + 1)
99
+ temp = temp >> 1;
100
+ end
101
+ endfunction
102
+
103
+ /** 主fifo **/
104
+ wire m_fifo_ren;
105
+ wire[fifo_data_width-1:0] m_fifo_dout;
106
+ wire m_fifo_empty_n;
107
+
108
+ generate
109
+ if(fwft_mode == "true")
110
+ begin
111
+ // FWFT模式
112
+ fifo_based_on_ram_std #(
113
+ .fifo_depth(fifo_depth),
114
+ .fifo_data_width(fifo_data_width),
115
+ .almost_full_th(almost_full_th),
116
+ .almost_empty_th(almost_empty_th),
117
+ .simulation_delay(simulation_delay)
118
+ )fifo_based_on_ram_std_u(
119
+ .clk(clk),
120
+ .rst_n(rst_n),
121
+ .fifo_wen(fifo_wen),
122
+ .fifo_din(fifo_din),
123
+ .fifo_full(fifo_full),
124
+ .fifo_full_n(fifo_full_n),
125
+ .fifo_almost_full(fifo_almost_full),
126
+ .fifo_almost_full_n(fifo_almost_full_n),
127
+ .fifo_ren(m_fifo_ren),
128
+ .fifo_dout(m_fifo_dout),
129
+ .fifo_empty(),
130
+ .fifo_empty_n(m_fifo_empty_n),
131
+ .fifo_almost_empty(fifo_almost_empty),
132
+ .fifo_almost_empty_n(fifo_almost_empty_n),
133
+ .ram_wen(ram_wen),
134
+ .ram_w_addr(ram_w_addr),
135
+ .ram_din(ram_din),
136
+ .ram_ren(ram_ren),
137
+ .ram_r_addr(ram_r_addr),
138
+ .ram_dout(ram_dout),
139
+ .data_cnt(data_cnt)
140
+ );
141
+ end
142
+ else
143
+ begin
144
+ // 标准模式
145
+ fifo_based_on_ram_std #(
146
+ .fifo_depth(fifo_depth),
147
+ .fifo_data_width(fifo_data_width),
148
+ .almost_full_th(almost_full_th),
149
+ .almost_empty_th(almost_empty_th),
150
+ .simulation_delay(simulation_delay)
151
+ )fifo_based_on_ram_std_u(
152
+ .clk(clk),
153
+ .rst_n(rst_n),
154
+ .fifo_wen(fifo_wen),
155
+ .fifo_din(fifo_din),
156
+ .fifo_full(fifo_full),
157
+ .fifo_full_n(fifo_full_n),
158
+ .fifo_almost_full(fifo_almost_full),
159
+ .fifo_almost_full_n(fifo_almost_full_n),
160
+ .fifo_ren(fifo_ren),
161
+ .fifo_dout(fifo_dout),
162
+ .fifo_empty(fifo_empty),
163
+ .fifo_empty_n(fifo_empty_n),
164
+ .fifo_almost_empty(fifo_almost_empty),
165
+ .fifo_almost_empty_n(fifo_almost_empty_n),
166
+ .ram_wen(ram_wen),
167
+ .ram_w_addr(ram_w_addr),
168
+ .ram_din(ram_din),
169
+ .ram_ren(ram_ren),
170
+ .ram_r_addr(ram_r_addr),
171
+ .ram_dout(ram_dout),
172
+ .data_cnt(data_cnt)
173
+ );
174
+ end
175
+ endgenerate
176
+
177
+ /** 从fifo(仅FWFT模式下需要) **/
178
+ generate
179
+ if(fwft_mode == "true")
180
+ begin
181
+ fifo_show_ahead_buffer #(
182
+ .fifo_data_width(fifo_data_width),
183
+ .simulation_delay(simulation_delay)
184
+ )fifo_show_ahead_buffer_u(
185
+ .clk(clk),
186
+ .rst_n(rst_n),
187
+
188
+ .std_fifo_ren(m_fifo_ren),
189
+ .std_fifo_dout(m_fifo_dout),
190
+ .std_fifo_empty(~m_fifo_empty_n),
191
+
192
+ .fwft_fifo_ren(fifo_ren),
193
+ .fwft_fifo_dout(fifo_dout),
194
+ .fwft_fifo_empty(fifo_empty),
195
+ .fwft_fifo_empty_n(fifo_empty_n)
196
+ );
197
+ end
198
+ endgenerate
199
+
200
+ endmodule
CutestPanda_Opensoc/101_apb_uart/rtl/fifo_based_on_lutram.v ADDED
@@ -0,0 +1,235 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ MIT License
3
+
4
+ Copyright (c) 2024 Panda, 2257691535@qq.com
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
23
+ */
24
+
25
+ `timescale 1ns / 1ps
26
+ /********************************************************************
27
+ 本模块: 基于lutram的同步fifo
28
+
29
+ 描述:
30
+ 全流水的高性能同步fifo
31
+ 基于lutram
32
+ 支持first word fall through特性(READ LA = 0)
33
+ 可选的固定阈值将满/将空信号
34
+
35
+ 注意:
36
+ 将满信号当存储计数 >= almost_full_th时有效
37
+ 将空信号当存储计数 <= almost_empty_th时有效
38
+ almost_full_th和almost_empty_th必须在[1, fifo_depth-1]范围内
39
+
40
+ 协议:
41
+ FIFO WRITE/READ
42
+
43
+ 作者: 陈家耀
44
+ 日期: 2023/10/13
45
+ ********************************************************************/
46
+
47
+
48
+ module fifo_based_on_lutram #(
49
+ parameter fwft_mode = "true", // 是否启用first word fall through特性
50
+ parameter integer fifo_depth = 32, // fifo深度(必须为2|4|8|16|...)
51
+ parameter integer fifo_data_width = 32, // fifo位宽
52
+ parameter integer almost_full_th = 20, // fifo将满阈值
53
+ parameter integer almost_empty_th = 5, // fifo将空阈值
54
+ parameter real simulation_delay = 1 // 仿真延时
55
+ )(
56
+ // 时钟和复位
57
+ input wire clk,
58
+ input wire rst_n,
59
+
60
+ // FIFO WRITE(fifo写端口)
61
+ input wire fifo_wen,
62
+ input wire[fifo_data_width-1:0] fifo_din,
63
+ output wire fifo_full,
64
+ output wire fifo_full_n,
65
+ output wire fifo_almost_full,
66
+ output wire fifo_almost_full_n,
67
+
68
+ // FIFO READ(fifo读端口)
69
+ input wire fifo_ren,
70
+ output wire[fifo_data_width-1:0] fifo_dout,
71
+ output wire fifo_empty,
72
+ output wire fifo_empty_n,
73
+ output wire fifo_almost_empty,
74
+ output wire fifo_almost_empty_n,
75
+
76
+ // 存储计数
77
+ output wire[clogb2(fifo_depth):0] data_cnt
78
+ );
79
+
80
+ // 计算log2(bit_depth)
81
+ function integer clogb2 (input integer bit_depth);
82
+ integer temp;
83
+ begin
84
+ temp = bit_depth;
85
+ for(clogb2 = -1;temp > 0;clogb2 = clogb2 + 1)
86
+ temp = temp >> 1;
87
+ end
88
+ endfunction
89
+
90
+ /** 参数 **/
91
+ localparam integer use_cnt_th = 8;
92
+
93
+ /** 空满标志和存储计数 **/
94
+ reg fifo_empty_reg;
95
+ reg fifo_full_reg;
96
+ reg fifo_almost_empty_reg;
97
+ reg fifo_almost_full_reg;
98
+ reg fifo_empty_n_reg;
99
+ reg fifo_full_n_reg;
100
+ reg fifo_almost_empty_n_reg;
101
+ reg fifo_almost_full_n_reg;
102
+ reg[clogb2(fifo_depth):0] data_cnt_regs;
103
+ reg[fifo_depth:0] data_cnt_onehot_regs;
104
+
105
+ assign {fifo_empty, fifo_full} = {fifo_empty_reg, fifo_full_reg};
106
+ assign {fifo_empty_n, fifo_full_n} = {fifo_empty_n_reg, fifo_full_n_reg};
107
+ assign {fifo_almost_empty, fifo_almost_full} = {fifo_almost_empty_reg, fifo_almost_full_reg};
108
+ assign {fifo_almost_empty_n, fifo_almost_full_n} = {fifo_almost_empty_n_reg, fifo_almost_full_n_reg};
109
+ assign data_cnt = data_cnt_regs;
110
+
111
+ always @(posedge clk or negedge rst_n)
112
+ begin
113
+ if(~rst_n)
114
+ begin
115
+ fifo_empty_reg <= 1'b1;
116
+ fifo_empty_n_reg <= 1'b0;
117
+ fifo_full_reg <= 1'b0;
118
+ fifo_full_n_reg <= 1'b1;
119
+ fifo_almost_empty_reg <= 1'b1;
120
+ fifo_almost_empty_n_reg <= 1'b0;
121
+ fifo_almost_full_reg <= 1'b0;
122
+ fifo_almost_full_n_reg <= 1'b1;
123
+
124
+ data_cnt_regs <= 0;
125
+ data_cnt_onehot_regs <= 1;
126
+ end
127
+ else if((fifo_wen & fifo_full_n_reg) ^ (fifo_ren & fifo_empty_n_reg))
128
+ begin
129
+ # simulation_delay;
130
+
131
+ if(fifo_wen & fifo_full_n_reg)
132
+ begin
133
+ // fifo数据增加1个
134
+ fifo_empty_reg <= 1'b0;
135
+ fifo_empty_n_reg <= 1'b1;
136
+ fifo_full_reg <= (fifo_depth >= use_cnt_th) ? data_cnt_regs == fifo_depth - 1:data_cnt_onehot_regs[fifo_depth-1];
137
+ fifo_full_n_reg <= (fifo_depth >= use_cnt_th) ? data_cnt_regs != fifo_depth - 1:(~data_cnt_onehot_regs[fifo_depth-1]);
138
+ fifo_almost_empty_reg <= (data_cnt_regs <= almost_empty_th - 1);
139
+ fifo_almost_empty_n_reg <= ~(data_cnt_regs <= almost_empty_th - 1);
140
+ fifo_almost_full_reg <= (data_cnt_regs >= almost_full_th - 1);
141
+ fifo_almost_full_n_reg <= ~(data_cnt_regs >= almost_full_th - 1);
142
+
143
+ data_cnt_regs <= data_cnt_regs + 1;
144
+ data_cnt_onehot_regs <= {data_cnt_onehot_regs[fifo_depth-1:0], 1'b0}; // 左移
145
+ end
146
+ else
147
+ begin
148
+ // fifo数据减少1个
149
+ fifo_empty_reg <= (fifo_depth >= use_cnt_th) ? data_cnt_regs == 1:data_cnt_onehot_regs[1];
150
+ fifo_empty_n_reg <= (fifo_depth >= use_cnt_th) ? data_cnt_regs != 1:(~data_cnt_onehot_regs[1]);
151
+ fifo_full_reg <= 1'b0;
152
+ fifo_full_n_reg <= 1'b1;
153
+ fifo_almost_empty_reg <= (data_cnt_regs <= almost_empty_th + 1);
154
+ fifo_almost_empty_n_reg <= ~(data_cnt_regs <= almost_empty_th + 1);
155
+ fifo_almost_full_reg <= (data_cnt_regs >= almost_full_th + 1);
156
+ fifo_almost_full_n_reg <= ~(data_cnt_regs >= almost_full_th + 1);
157
+
158
+ data_cnt_regs <= data_cnt_regs - 1;
159
+ data_cnt_onehot_regs <= {1'b0, data_cnt_onehot_regs[fifo_depth:1]}; // 右移
160
+ end
161
+ end
162
+ end
163
+
164
+ /** 读写指针 **/
165
+ reg[clogb2(fifo_depth-1):0] fifo_rptr;
166
+ reg[clogb2(fifo_depth-1):0] fifo_rptr_add1;
167
+ reg[clogb2(fifo_depth-1):0] fifo_wptr;
168
+ reg[fifo_depth-1:0] fifo_wptr_onehot;
169
+
170
+ always @(posedge clk or negedge rst_n)
171
+ begin
172
+ if(~rst_n)
173
+ fifo_rptr <= 0;
174
+ else if(fifo_ren & fifo_empty_n_reg)
175
+ #simulation_delay fifo_rptr <= fifo_rptr + 1;
176
+ end
177
+
178
+ always @(posedge clk or negedge rst_n)
179
+ begin
180
+ if(~rst_n)
181
+ fifo_rptr_add1 <= 1;
182
+ else if(fifo_ren & fifo_empty_n_reg)
183
+ #simulation_delay fifo_rptr_add1 <= fifo_rptr_add1 + 1;
184
+ end
185
+
186
+ always @(posedge clk or negedge rst_n)
187
+ begin
188
+ if(~rst_n)
189
+ fifo_wptr <= 0;
190
+ else if(fifo_wen & fifo_full_n_reg)
191
+ #simulation_delay fifo_wptr <= fifo_wptr + 1;
192
+ end
193
+
194
+ always @(posedge clk or negedge rst_n)
195
+ begin
196
+ if(~rst_n)
197
+ fifo_wptr_onehot <= 1;
198
+ else if(fifo_wen & fifo_full_n_reg)
199
+ #simulation_delay fifo_wptr_onehot <= {fifo_wptr_onehot[fifo_depth-2:0], fifo_wptr_onehot[fifo_depth-1]}; // 循环左移
200
+ end
201
+
202
+ /** 读写数据 **/
203
+ (* ram_style="distributed" *) reg[fifo_data_width-1:0] fifo_regs[fifo_depth-1:0];
204
+ reg[fifo_data_width-1:0] fifo_dout_regs;
205
+
206
+ assign fifo_dout = fifo_dout_regs;
207
+
208
+ always @(posedge clk)
209
+ begin
210
+ if(fifo_wen & fifo_full_n_reg)
211
+ #simulation_delay fifo_regs[fifo_wptr] <= fifo_din;
212
+ end
213
+
214
+ generate
215
+ if(fwft_mode == "true")
216
+ begin
217
+ always @(posedge clk)
218
+ begin
219
+ if({fifo_empty_n_reg, fifo_ren} != 2'b10)
220
+ #simulation_delay fifo_dout_regs <= fifo_empty_n_reg & (~(fifo_wen & ((fifo_depth >= use_cnt_th) ? data_cnt_regs == 1:data_cnt_onehot_regs[1]))) ?
221
+ fifo_regs[fifo_rptr_add1]:fifo_din;
222
+ end
223
+ end
224
+ else
225
+ begin
226
+ always @(posedge clk)
227
+ begin
228
+ if(fifo_ren & fifo_empty_n_reg)
229
+ #simulation_delay fifo_dout_regs <= fifo_regs[fifo_rptr];
230
+ end
231
+ end
232
+ endgenerate
233
+
234
+ endmodule
235
+
CutestPanda_Opensoc/101_apb_uart/rtl/fifo_based_on_ram_std.v ADDED
@@ -0,0 +1,223 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ MIT License
3
+
4
+ Copyright (c) 2024 Panda, 2257691535@qq.com
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
23
+ */
24
+
25
+ `timescale 1ns / 1ps
26
+ /********************************************************************
27
+ 本模块: 基于ram的标准同步fifo控制器
28
+
29
+ 描述:
30
+ 全流水的高性能同步fifo控制器
31
+ 基于ram
32
+ 标准fifo(READ LA = 1|2)
33
+ 可选的固定阈值将满/将空信号
34
+
35
+ 注意:
36
+ 将满信号当存储计数 >= almost_full_th时有效
37
+ 将空信号当存储计数 <= almost_empty_th时有效
38
+ almost_full_th和almost_empty_th必须在[1, fifo_depth-1]范围内
39
+ 要求ram的读延迟=1clk
40
+
41
+ 协议:
42
+ FIFO WRITE/READ
43
+ MEM WRITE/READ
44
+
45
+ 作者: 陈家耀
46
+ 日期: 2023/10/29
47
+ ********************************************************************/
48
+
49
+
50
+ module fifo_based_on_ram_std #(
51
+ parameter integer fifo_depth = 32, // fifo深度(必须为2|4|8|16|...)
52
+ parameter integer fifo_data_width = 32, // fifo位宽
53
+ parameter integer almost_full_th = 20, // fifo将满阈值
54
+ parameter integer almost_empty_th = 5, // fifo将空阈值
55
+ parameter real simulation_delay = 1 // 仿真延时
56
+ )(
57
+ // 时钟和复位
58
+ input wire clk,
59
+ input wire rst_n,
60
+
61
+ // FIFO WRITE(fifo写端口)
62
+ input wire fifo_wen,
63
+ input wire[fifo_data_width-1:0] fifo_din,
64
+ output wire fifo_full,
65
+ output wire fifo_full_n,
66
+ output wire fifo_almost_full,
67
+ output wire fifo_almost_full_n,
68
+
69
+ // FIFO READ(fifo读端口)
70
+ input wire fifo_ren,
71
+ output wire[fifo_data_width-1:0] fifo_dout,
72
+ output wire fifo_empty,
73
+ output wire fifo_empty_n,
74
+ output wire fifo_almost_empty,
75
+ output wire fifo_almost_empty_n,
76
+
77
+ // MEM WRITE(ram写端口)
78
+ output wire ram_wen,
79
+ output wire[clogb2(fifo_depth-1):0] ram_w_addr,
80
+ output wire[fifo_data_width-1:0] ram_din,
81
+
82
+ // MEM RAD(ram读端口)
83
+ output wire ram_ren,
84
+ output wire[clogb2(fifo_depth-1):0] ram_r_addr,
85
+ input wire[fifo_data_width-1:0] ram_dout,
86
+
87
+ // 存储计数
88
+ output wire[clogb2(fifo_depth):0] data_cnt
89
+ );
90
+
91
+ // 计算log2(bit_depth)
92
+ function integer clogb2 (input integer bit_depth);
93
+ integer temp;
94
+ begin
95
+ temp = bit_depth;
96
+ for(clogb2 = -1;temp > 0;clogb2 = clogb2 + 1)
97
+ temp = temp >> 1;
98
+ end
99
+ endfunction
100
+
101
+ /** 参数 **/
102
+ localparam integer use_cnt_th = 8;
103
+
104
+ /** 空满标志和存储计数 **/
105
+ reg fifo_empty_reg;
106
+ reg fifo_full_reg;
107
+ reg fifo_almost_empty_reg;
108
+ reg fifo_almost_full_reg;
109
+ reg fifo_empty_n_reg;
110
+ reg fifo_full_n_reg;
111
+ reg fifo_almost_empty_n_reg;
112
+ reg fifo_almost_full_n_reg;
113
+ reg[clogb2(fifo_depth):0] data_cnt_regs;
114
+ reg[fifo_depth:0] data_cnt_onehot_regs;
115
+
116
+ assign {fifo_empty, fifo_full} = {fifo_empty_reg, fifo_full_reg};
117
+ assign {fifo_empty_n, fifo_full_n} = {fifo_empty_n_reg, fifo_full_n_reg};
118
+ assign {fifo_almost_empty, fifo_almost_full} = {fifo_almost_empty_reg, fifo_almost_full_reg};
119
+ assign {fifo_almost_empty_n, fifo_almost_full_n} = {fifo_almost_empty_n_reg, fifo_almost_full_n_reg};
120
+ assign data_cnt = data_cnt_regs;
121
+
122
+ always @(posedge clk or negedge rst_n)
123
+ begin
124
+ if(~rst_n)
125
+ begin
126
+ fifo_empty_reg <= 1'b1;
127
+ fifo_empty_n_reg <= 1'b0;
128
+ fifo_full_reg <= 1'b0;
129
+ fifo_full_n_reg <= 1'b1;
130
+ fifo_almost_empty_reg <= 1'b1;
131
+ fifo_almost_empty_n_reg <= 1'b0;
132
+ fifo_almost_full_reg <= 1'b0;
133
+ fifo_almost_full_n_reg <= 1'b1;
134
+
135
+ data_cnt_regs <= 0;
136
+ data_cnt_onehot_regs <= 1;
137
+ end
138
+ else if((fifo_wen & fifo_full_n_reg) ^ (fifo_ren & fifo_empty_n_reg))
139
+ begin
140
+ # simulation_delay;
141
+
142
+ if(fifo_wen & fifo_full_n_reg)
143
+ begin
144
+ // fifo数据增加1个
145
+ fifo_empty_reg <= 1'b0;
146
+ fifo_empty_n_reg <= 1'b1;
147
+ fifo_full_reg <= (fifo_depth >= use_cnt_th) ? data_cnt_regs == fifo_depth - 1:data_cnt_onehot_regs[fifo_depth-1];
148
+ fifo_full_n_reg <= (fifo_depth >= use_cnt_th) ? data_cnt_regs != fifo_depth - 1:(~data_cnt_onehot_regs[fifo_depth-1]);
149
+ fifo_almost_empty_reg <= (data_cnt_regs <= almost_empty_th - 1);
150
+ fifo_almost_empty_n_reg <= ~(data_cnt_regs <= almost_empty_th - 1);
151
+ fifo_almost_full_reg <= (data_cnt_regs >= almost_full_th - 1);
152
+ fifo_almost_full_n_reg <= ~(data_cnt_regs >= almost_full_th - 1);
153
+
154
+ data_cnt_regs <= data_cnt_regs + 1;
155
+ data_cnt_onehot_regs <= {data_cnt_onehot_regs[fifo_depth-1:0], 1'b0}; // 左移
156
+ end
157
+ else
158
+ begin
159
+ // fifo数据减少1个
160
+ fifo_empty_reg <= (fifo_depth >= use_cnt_th) ? data_cnt_regs == 1:data_cnt_onehot_regs[1];
161
+ fifo_empty_n_reg <= (fifo_depth >= use_cnt_th) ? data_cnt_regs != 1:(~data_cnt_onehot_regs[1]);
162
+ fifo_full_reg <= 1'b0;
163
+ fifo_full_n_reg <= 1'b1;
164
+ fifo_almost_empty_reg <= (data_cnt_regs <= almost_empty_th + 1);
165
+ fifo_almost_empty_n_reg <= ~(data_cnt_regs <= almost_empty_th + 1);
166
+ fifo_almost_full_reg <= (data_cnt_regs >= almost_full_th + 1);
167
+ fifo_almost_full_n_reg <= ~(data_cnt_regs >= almost_full_th + 1);
168
+
169
+ data_cnt_regs <= data_cnt_regs - 1;
170
+ data_cnt_onehot_regs <= {1'b0, data_cnt_onehot_regs[fifo_depth:1]}; // 右移
171
+ end
172
+ end
173
+ end
174
+
175
+ /** 读写指针 **/
176
+ reg[clogb2(fifo_depth-1):0] fifo_rptr;
177
+ reg[clogb2(fifo_depth-1):0] fifo_rptr_add1;
178
+ reg[clogb2(fifo_depth-1):0] fifo_wptr;
179
+ reg[fifo_depth-1:0] fifo_wptr_onehot;
180
+
181
+ always @(posedge clk or negedge rst_n)
182
+ begin
183
+ if(~rst_n)
184
+ fifo_rptr <= 0;
185
+ else if(fifo_ren & fifo_empty_n_reg)
186
+ #simulation_delay fifo_rptr <= fifo_rptr + 1;
187
+ end
188
+
189
+ always @(posedge clk or negedge rst_n)
190
+ begin
191
+ if(~rst_n)
192
+ fifo_rptr_add1 <= 1;
193
+ else if(fifo_ren & fifo_empty_n_reg)
194
+ #simulation_delay fifo_rptr_add1 <= fifo_rptr_add1 + 1;
195
+ end
196
+
197
+ always @(posedge clk or negedge rst_n)
198
+ begin
199
+ if(~rst_n)
200
+ fifo_wptr <= 0;
201
+ else if(fifo_wen & fifo_full_n_reg)
202
+ #simulation_delay fifo_wptr <= fifo_wptr + 1;
203
+ end
204
+
205
+ always @(posedge clk or negedge rst_n)
206
+ begin
207
+ if(~rst_n)
208
+ fifo_wptr_onehot <= 1;
209
+ else if(fifo_wen & fifo_full_n_reg)
210
+ #simulation_delay fifo_wptr_onehot <= {fifo_wptr_onehot[fifo_depth-2:0], fifo_wptr_onehot[fifo_depth-1]}; // 循环左移
211
+ end
212
+
213
+ /** 读写数据 **/
214
+ assign ram_wen = fifo_wen & fifo_full_n_reg;
215
+ assign ram_w_addr = fifo_wptr;
216
+ assign ram_din = fifo_din;
217
+
218
+ assign ram_ren = fifo_ren & fifo_empty_n_reg;
219
+ assign ram_r_addr = fifo_rptr;
220
+ assign fifo_dout = ram_dout;
221
+
222
+ endmodule
223
+
CutestPanda_Opensoc/101_apb_uart/rtl/fifo_show_ahead_buffer.v ADDED
@@ -0,0 +1,122 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ MIT License
3
+
4
+ Copyright (c) 2024 Panda, 2257691535@qq.com
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
23
+ */
24
+
25
+ `timescale 1ns / 1ps
26
+ /********************************************************************
27
+ 本模块: fifo FWFT缓冲区
28
+
29
+ 描述:
30
+ 用于将读延迟为1clk的fifo转换为FWFT的fifo
31
+
32
+ 注意:
33
+
34
+
35
+ 协议:
36
+ FIFO READ
37
+
38
+ 作者: 陈家耀
39
+ 日期: 2024/05/30
40
+ ********************************************************************/
41
+
42
+
43
+ module fifo_show_ahead_buffer #(
44
+ parameter integer fifo_data_width = 32, // fifo位宽
45
+ parameter real simulation_delay = 1 // 仿真延时
46
+ )(
47
+ // 时钟和复位
48
+ input wire clk,
49
+ input wire rst_n,
50
+
51
+ // 标准fifo的读端口
52
+ output wire std_fifo_ren,
53
+ input wire[fifo_data_width-1:0] std_fifo_dout,
54
+ input wire std_fifo_empty,
55
+
56
+ // FWFT fifo的读端口
57
+ input wire fwft_fifo_ren,
58
+ output wire[fifo_data_width-1:0] fwft_fifo_dout,
59
+ output wire fwft_fifo_empty,
60
+ output wire fwft_fifo_empty_n
61
+ );
62
+
63
+ /** 寄存器buffer **/
64
+ reg std_fifo_rvld; // 标准fifo有效读(指示)
65
+ reg[fifo_data_width-1:0] regs_buffer[1:0]; // 寄存器buffer
66
+ reg[2:0] buffer_data_cnt; // buffer存储计数(3'b001 -> 0, 3'b010 -> 1, 3'b100 -> 2)
67
+ wire[2:0] buffer_data_cnt_sub1; // buffer存储计数 - 1(3'b001 -> 0, 3'b010 -> 1, 3'b100 -> 2)
68
+
69
+ assign buffer_data_cnt_sub1 = {buffer_data_cnt[0], buffer_data_cnt[2:1]};
70
+
71
+ // 标准fifo有效读(指示)
72
+ always @(posedge clk or negedge rst_n)
73
+ begin
74
+ if(~rst_n)
75
+ std_fifo_rvld <= 1'b0;
76
+ else
77
+ # simulation_delay std_fifo_rvld <= std_fifo_ren & (~std_fifo_empty);
78
+ end
79
+
80
+ // 寄存器buffer
81
+ always @(posedge clk)
82
+ begin
83
+ if(std_fifo_rvld & ((fwft_fifo_ren & fwft_fifo_empty_n) ? buffer_data_cnt_sub1[0]:buffer_data_cnt[0]))
84
+ # simulation_delay regs_buffer[0] <= std_fifo_dout;
85
+ else if(fwft_fifo_ren & fwft_fifo_empty_n)
86
+ # simulation_delay regs_buffer[0] <= regs_buffer[1];
87
+ end
88
+
89
+ always @(posedge clk)
90
+ begin
91
+ if(std_fifo_rvld & ((fwft_fifo_ren & fwft_fifo_empty_n) ? buffer_data_cnt_sub1[1]:buffer_data_cnt[1]))
92
+ # simulation_delay regs_buffer[1] <= std_fifo_dout;
93
+ end
94
+
95
+ // buffer存储计数独热码
96
+ always @(posedge clk or negedge rst_n)
97
+ begin
98
+ if(~rst_n)
99
+ buffer_data_cnt <= 3'b001;
100
+ else if(std_fifo_rvld ^ (fwft_fifo_ren & fwft_fifo_empty_n))
101
+ # simulation_delay buffer_data_cnt <= std_fifo_rvld ? {buffer_data_cnt[1:0], buffer_data_cnt[2]}:{buffer_data_cnt[0], buffer_data_cnt[2:1]};
102
+ end
103
+
104
+ /** 标准fifo的读端口 **/
105
+ assign std_fifo_ren = buffer_data_cnt[0] | fwft_fifo_ren; // buffer存储计数==0或者FWFT-fifo读使能时标准fifo读使能有效
106
+
107
+ /** FWFT fifo的读端口 **/
108
+ reg fwft_fifo_empty_n_reg;
109
+
110
+ assign fwft_fifo_dout = regs_buffer[0];
111
+ assign fwft_fifo_empty = buffer_data_cnt[0];
112
+ assign fwft_fifo_empty_n = fwft_fifo_empty_n_reg;
113
+
114
+ always @(posedge clk or negedge rst_n)
115
+ begin
116
+ if(~rst_n)
117
+ fwft_fifo_empty_n_reg <= 1'b0;
118
+ else if(std_fifo_rvld ^ (fwft_fifo_ren & fwft_fifo_empty_n))
119
+ # simulation_delay fwft_fifo_empty_n_reg <= std_fifo_rvld ? 1'b1:(~buffer_data_cnt[1]);
120
+ end
121
+
122
+ endmodule
CutestPanda_Opensoc/101_apb_uart/rtl/itr_generator.v ADDED
@@ -0,0 +1,104 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ MIT License
3
+
4
+ Copyright (c) 2024 Panda, 2257691535@qq.com
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
23
+ */
24
+
25
+ `timescale 1ns / 1ps
26
+ /********************************************************************
27
+ 本模块: 中断信号发生器
28
+
29
+ 描述:
30
+ 将原始的中断脉冲拓展为一定脉宽的中断信号
31
+
32
+ 注意:
33
+ 中断脉宽必须 >= 2
34
+
35
+ 协议:
36
+
37
+
38
+ 作者: 陈家耀
39
+ 日期: 2023/11/08
40
+ ********************************************************************/
41
+
42
+
43
+ module itr_generator #(
44
+ parameter integer pulse_w = 100, // 中断脉宽(必须>=1)
45
+ parameter real simulation_delay = 1 // 仿真延时
46
+ )(
47
+ // 时钟和复位
48
+ input wire clk,
49
+ input wire rst_n,
50
+
51
+ input wire itr_org, // 原始中断输入
52
+ output wire itr // 中断输出
53
+ );
54
+
55
+ // 计算log2(bit_depth)
56
+ function integer clogb2 (input integer bit_depth);
57
+ integer temp;
58
+ begin
59
+ temp = bit_depth;
60
+ for(clogb2 = -1;temp > 0;clogb2 = clogb2 + 1)
61
+ temp = temp >> 1;
62
+ end
63
+ endfunction
64
+
65
+ // 产生中断信号
66
+ reg itr_reg; // 中断信号
67
+ reg[clogb2(pulse_w - 1):0] itr_cnt; // 中断计数器
68
+
69
+ assign itr = itr_reg;
70
+
71
+ generate
72
+ if(pulse_w == 1)
73
+ begin
74
+ always @(posedge clk or negedge rst_n)
75
+ begin
76
+ if(~rst_n)
77
+ itr_reg <= 1'b0;
78
+ else
79
+ # simulation_delay itr_reg <= itr_org;
80
+ end
81
+ end
82
+ else
83
+ begin
84
+ always @(posedge clk or negedge rst_n)
85
+ begin
86
+ if(~rst_n)
87
+ itr_reg <= 1'b0;
88
+ else
89
+ begin
90
+ # simulation_delay;
91
+
92
+ if(~itr_reg) // 等待原始中断脉冲
93
+ itr_reg <= itr_org;
94
+ else // 等待计数完成
95
+ itr_reg <= itr_cnt != pulse_w - 1;
96
+ end
97
+ end
98
+ end
99
+ endgenerate
100
+
101
+ always @(posedge clk)
102
+ # simulation_delay itr_cnt <= (~itr_reg) ? 0:(itr_cnt + 1);
103
+
104
+ endmodule
CutestPanda_Opensoc/101_apb_uart/rtl/ram_fifo_wrapper.v ADDED
@@ -0,0 +1,220 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ MIT License
3
+
4
+ Copyright (c) 2024 Panda, 2257691535@qq.com
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
23
+ */
24
+
25
+ `timescale 1ns / 1ps
26
+ /********************************************************************
27
+ 本模块: 基于lutram或bram的同步fifo
28
+
29
+ 描述:
30
+ 全流水的高性能同步fifo
31
+ 基于lutram或bram
32
+ 支持first word fall through特性(READ LA = 0)
33
+ 可选的固定阈值将满/将空信号
34
+ 可选的存储计数输出
35
+
36
+ 注意:
37
+ 将满信号当存储计数 >= almost_full_th时有效
38
+ 将空信号当存储计数 <= almost_empty_th时有效
39
+ almost_full_th和almost_empty_th必须在[1, fifo_depth-1]范围内
40
+ 本模块用于fpga, 子模块fifo_based_on_ram可用于asic设计
41
+ 寄存器fifo请用子模块fifo_based_on_regs
42
+
43
+ 协议:
44
+ FIFO WRITE/READ
45
+
46
+ 作者: 陈家耀
47
+ 日期: 2023/10/29
48
+ ********************************************************************/
49
+
50
+
51
+ module ram_fifo_wrapper #(
52
+ parameter fwft_mode = "true", // 是否启用first word fall through特性
53
+ parameter ram_type = "lutram", // RAM类型(lutram|bram)
54
+ parameter en_bram_reg = "false", // 是否启用BRAM输出寄存器
55
+ parameter integer fifo_depth = 32, // fifo深度(必须为2|4|8|16|...)
56
+ parameter integer fifo_data_width = 32, // fifo位宽
57
+ parameter full_assert_polarity = "low", // 满信号有效极性(low|high)
58
+ parameter empty_assert_polarity = "low", // 空信号有效极性(low|high)
59
+ parameter almost_full_assert_polarity = "no", // 将满信号有效极性(low|high|no)
60
+ parameter almost_empty_assert_polarity = "no", // 将空信号有效极性(low|high|no)
61
+ parameter en_data_cnt = "false", // 是否启用存储计数器
62
+ parameter integer almost_full_th = 20, // fifo将满阈值
63
+ parameter integer almost_empty_th = 5, // fifo将空阈值
64
+ parameter real simulation_delay = 1 // 仿真延时
65
+ )(
66
+ // 时钟和复位
67
+ input wire clk,
68
+ input wire rst_n,
69
+
70
+ // FIFO WRITE(fifo写端口)
71
+ input wire fifo_wen,
72
+ input wire[fifo_data_width-1:0] fifo_din,
73
+ output wire fifo_full,
74
+ output wire fifo_full_n,
75
+ output wire fifo_almost_full,
76
+ output wire fifo_almost_full_n,
77
+
78
+ // FIFO READ(fifo读端口)
79
+ input wire fifo_ren,
80
+ output wire[fifo_data_width-1:0] fifo_dout,
81
+ output wire fifo_empty,
82
+ output wire fifo_empty_n,
83
+ output wire fifo_almost_empty,
84
+ output wire fifo_almost_empty_n,
85
+
86
+ // 存储计数
87
+ output wire[clogb2(fifo_depth):0] data_cnt
88
+ );
89
+
90
+ // 计算log2(bit_depth)
91
+ function integer clogb2 (input integer bit_depth);
92
+ integer temp;
93
+ begin
94
+ temp = bit_depth;
95
+ for(clogb2 = -1;temp > 0;clogb2 = clogb2 + 1)
96
+ temp = temp >> 1;
97
+ end
98
+ endfunction
99
+
100
+ wire fifo_full_w;
101
+ wire fifo_full_n_w;
102
+ wire fifo_almost_full_w;
103
+ wire fifo_almost_full_n_w;
104
+ wire fifo_empty_w;
105
+ wire fifo_empty_n_w;
106
+ wire fifo_almost_empty_w;
107
+ wire fifo_almost_empty_n_w;
108
+ wire[clogb2(fifo_depth):0] data_cnt_w;
109
+
110
+ generate
111
+ if(full_assert_polarity == "low")
112
+ assign fifo_full_n = fifo_full_n_w;
113
+ else
114
+ assign fifo_full = fifo_full_w;
115
+
116
+ if(almost_full_assert_polarity == "low")
117
+ assign fifo_almost_full_n = fifo_almost_full_n_w;
118
+ else if(almost_full_assert_polarity == "high")
119
+ assign fifo_almost_full = fifo_almost_full_w;
120
+
121
+ if(empty_assert_polarity == "low")
122
+ assign fifo_empty_n = fifo_empty_n_w;
123
+ else
124
+ assign fifo_empty = fifo_empty_w;
125
+
126
+ if(almost_empty_assert_polarity == "low")
127
+ assign fifo_almost_empty_n = fifo_almost_empty_n_w;
128
+ else if(almost_empty_assert_polarity == "high")
129
+ assign fifo_almost_empty = fifo_almost_empty_w;
130
+
131
+ if(en_data_cnt == "true")
132
+ assign data_cnt = data_cnt_w;
133
+
134
+ if(ram_type == "lutram")
135
+ begin
136
+ fifo_based_on_lutram #(
137
+ .fwft_mode(fwft_mode),
138
+ .fifo_depth(fifo_depth),
139
+ .fifo_data_width(fifo_data_width),
140
+ .almost_full_th(almost_full_th),
141
+ .almost_empty_th(almost_empty_th),
142
+ .simulation_delay(simulation_delay)
143
+ )fifo(
144
+ .clk(clk),
145
+ .rst_n(rst_n),
146
+ .fifo_wen(fifo_wen),
147
+ .fifo_din(fifo_din),
148
+ .fifo_full(fifo_full_w),
149
+ .fifo_full_n(fifo_full_n_w),
150
+ .fifo_almost_full(fifo_almost_full_w),
151
+ .fifo_almost_full_n(fifo_almost_full_n_w),
152
+ .fifo_ren(fifo_ren),
153
+ .fifo_dout(fifo_dout),
154
+ .fifo_empty(fifo_empty_w),
155
+ .fifo_empty_n(fifo_empty_n_w),
156
+ .fifo_almost_empty(fifo_almost_empty_w),
157
+ .fifo_almost_empty_n(fifo_almost_empty_n_w),
158
+ .data_cnt(data_cnt_w)
159
+ );
160
+ end
161
+ else
162
+ begin
163
+ wire ram_wen_a;
164
+ wire[clogb2(fifo_depth-1):0] ram_addr_a;
165
+ wire[fifo_data_width-1:0] ram_din_a;
166
+ wire ram_ren_b;
167
+ wire[clogb2(fifo_depth-1):0] ram_addr_b;
168
+ wire[fifo_data_width-1:0] ram_dout_b;
169
+
170
+ fifo_based_on_ram #(
171
+ .fwft_mode(fwft_mode),
172
+ .ram_read_la((en_bram_reg == "true") ? 2:1),
173
+ .fifo_depth(fifo_depth),
174
+ .fifo_data_width(fifo_data_width),
175
+ .almost_full_th(almost_full_th),
176
+ .almost_empty_th(almost_empty_th),
177
+ .simulation_delay(simulation_delay)
178
+ )fifo_ctrler(
179
+ .clk(clk),
180
+ .rst_n(rst_n),
181
+ .fifo_wen(fifo_wen),
182
+ .fifo_din(fifo_din),
183
+ .fifo_full(fifo_full_w),
184
+ .fifo_full_n(fifo_full_n_w),
185
+ .fifo_almost_full(fifo_almost_full_w),
186
+ .fifo_almost_full_n(fifo_almost_full_n_w),
187
+ .fifo_ren(fifo_ren),
188
+ .fifo_dout(fifo_dout),
189
+ .fifo_empty(fifo_empty_w),
190
+ .fifo_empty_n(fifo_empty_n_w),
191
+ .fifo_almost_empty(fifo_almost_empty_w),
192
+ .fifo_almost_empty_n(fifo_almost_empty_n_w),
193
+ .ram_wen(ram_wen_a),
194
+ .ram_w_addr(ram_addr_a),
195
+ .ram_din(ram_din_a),
196
+ .ram_ren(ram_ren_b),
197
+ .ram_r_addr(ram_addr_b),
198
+ .ram_dout(ram_dout_b),
199
+ .data_cnt(data_cnt_w)
200
+ );
201
+
202
+ bram_simple_dual_port #(
203
+ .style((en_bram_reg == "true") ? "HIGH_PERFORMANCE":"LOW_LATENCY"),
204
+ .mem_width(fifo_data_width),
205
+ .mem_depth(fifo_depth),
206
+ .INIT_FILE("no_init"),
207
+ .simulation_delay(simulation_delay)
208
+ )fifo_ram(
209
+ .clk(clk),
210
+ .wen_a(ram_wen_a),
211
+ .addr_a(ram_addr_a),
212
+ .din_a(ram_din_a),
213
+ .ren_b(ram_ren_b),
214
+ .addr_b(ram_addr_b),
215
+ .dout_b(ram_dout_b)
216
+ );
217
+ end
218
+ endgenerate
219
+
220
+ endmodule
CutestPanda_Opensoc/101_apb_uart/rtl/uart_rx.v ADDED
@@ -0,0 +1,263 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ MIT License
3
+
4
+ Copyright (c) 2024 Panda, 2257691535@qq.com
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
23
+ */
24
+
25
+ `timescale 1ns / 1ps
26
+ /********************************************************************
27
+ 本模块: UART接收控制器
28
+
29
+ 描述:
30
+ 符合AXIS协议的UART接收控制器
31
+
32
+ 注意:
33
+ UART接收可能溢出
34
+
35
+ 协议:
36
+ AXIS MASTER
37
+ UART
38
+
39
+ 作者: 陈家耀
40
+ 日期: 2023/11/08
41
+ ********************************************************************/
42
+
43
+
44
+ module uart_rx #(
45
+ parameter integer clk_frequency = 200000000, // 时钟频率
46
+ parameter integer baud_rate = 115200, // 波特率
47
+ parameter real simulation_delay = 1 // 仿真延时
48
+ )(
49
+ input wire clk,
50
+ input wire rst_n,
51
+
52
+ input wire rx,
53
+
54
+ output wire[7:0] rx_byte_data,
55
+ output wire rx_byte_valid,
56
+ input wire rx_byte_ready,
57
+
58
+ output wire rx_idle,
59
+ output wire rx_done,
60
+ output wire rx_start
61
+ );
62
+
63
+ // 计算log2(bit_depth)
64
+ function integer clogb2 (input integer bit_depth);
65
+ integer temp;
66
+ begin
67
+ temp = bit_depth;
68
+ for(clogb2 = -1;temp > 0;clogb2 = clogb2 + 1)
69
+ temp = temp >> 1;
70
+ end
71
+ endfunction
72
+
73
+ localparam integer clk_n_per_bit = clk_frequency / baud_rate; // 传输每一位所用的时钟周期个数
74
+
75
+ /** 打两拍消除亚稳态 **/
76
+ reg rx_d;
77
+ reg rx_d2;
78
+ wire rx_stable;
79
+
80
+ assign rx_stable = rx_d2;
81
+
82
+ always @(posedge clk or negedge rst_n)
83
+ begin
84
+ if(~rst_n)
85
+ {rx_d2, rx_d} <= 2'b11;
86
+ else
87
+ # simulation_delay {rx_d2, rx_d} <= {rx_d, rx};
88
+ end
89
+
90
+ /** 检测下降沿 **/
91
+ reg rx_stable_d;
92
+ reg rx_neg_edge_detected; // 检测到下降沿(脉冲)
93
+
94
+ always @(posedge clk or negedge rst_n)
95
+ begin
96
+ if(~rst_n)
97
+ rx_stable_d <= 1'b1;
98
+ else
99
+ # simulation_delay rx_stable_d <= rx_stable;
100
+ end
101
+
102
+ always @(posedge clk or negedge rst_n)
103
+ begin
104
+ if(~rst_n)
105
+ rx_neg_edge_detected <= 1'b0;
106
+ else
107
+ # simulation_delay rx_neg_edge_detected <= (~rx_stable) & rx_stable_d;
108
+ end
109
+
110
+ /** 数据接收状态机 **/
111
+ localparam status_idle = 2'b00; // 状态:空闲
112
+ localparam status_start = 2'b01; // 状态:起始位
113
+ localparam status_data = 2'b10; // 状态:数据位
114
+ localparam status_stop = 2'b11; // 状态:停止位
115
+
116
+ reg[1:0] now_status;
117
+
118
+ reg[clogb2(clk_n_per_bit-1):0] cnt; // 波特率计数器
119
+ reg[2:0] now_bit_i; // 当前接收字节位编号
120
+ reg[7:0] byte; // 当前接收的字节
121
+ reg byte_vld; // 接收字节完成(脉冲)
122
+ reg rx_idle_reg; // UART控制器接收空闲(标志)
123
+
124
+ reg cnt_eq_clk_n_per_bit_div2_sub1; // 波特率计数器 == UART位时钟周期 / 2 - 1(脉冲)
125
+ reg cnt_eq_clk_n_per_bit_sub1; // 波特率计数器 == UART位时钟周期 - 1(脉冲)
126
+
127
+ assign rx_byte_data = byte;
128
+ assign rx_byte_valid = byte_vld;
129
+ assign rx_idle = rx_idle_reg;
130
+ assign rx_done = byte_vld;
131
+ assign rx_start = rx_neg_edge_detected;
132
+
133
+ // 波特率计数器比较结果指示
134
+ always @(posedge clk or negedge rst_n)
135
+ begin
136
+ if(~rst_n)
137
+ begin
138
+ cnt_eq_clk_n_per_bit_div2_sub1 <= 1'b0;
139
+ cnt_eq_clk_n_per_bit_sub1 <= 1'b0;
140
+ end
141
+ else
142
+ begin
143
+ # simulation_delay;
144
+
145
+ cnt_eq_clk_n_per_bit_div2_sub1 <= (cnt == clk_n_per_bit / 2 - 2);
146
+ cnt_eq_clk_n_per_bit_sub1 <= (cnt == clk_n_per_bit - 2);
147
+ end
148
+ end
149
+
150
+ always @(posedge clk or negedge rst_n)
151
+ begin
152
+ if(~rst_n)
153
+ begin
154
+ now_status <= status_idle;
155
+ cnt <= 0;
156
+ now_bit_i <= 3'b000;
157
+ byte_vld <= 1'b0;
158
+ rx_idle_reg <= 1'b1;
159
+ end
160
+ else
161
+ begin
162
+ # simulation_delay;
163
+
164
+ byte_vld <= 1'b0;
165
+ case(now_status)
166
+ status_idle: // 状态:空闲
167
+ begin
168
+ rx_idle_reg <= rx_idle_reg | cnt_eq_clk_n_per_bit_div2_sub1; // 为了跳过上一次传输的停止位, idle信号只能在波特率计数器 == UART位时钟周期 / 2 - 1时置为有效
169
+
170
+ if(rx_neg_edge_detected) // 检测到开始位
171
+ begin
172
+ now_status <= status_start;
173
+ cnt <= 0;
174
+ end
175
+ else
176
+ begin
177
+ now_status <= status_idle;
178
+ cnt <= cnt + 1;
179
+ end
180
+ end
181
+ status_start: // 状态:起始位
182
+ begin
183
+ rx_idle_reg <= 1'b0;
184
+
185
+ if(cnt_eq_clk_n_per_bit_div2_sub1) // 对齐到起始位中央再继续接收
186
+ begin
187
+ now_status <= status_data;
188
+ cnt <= 0;
189
+ end
190
+ else
191
+ begin
192
+ now_status <= status_start;
193
+ cnt <= cnt + 1;
194
+ end
195
+ end
196
+ status_data: // 状态:数据位
197
+ begin
198
+ rx_idle_reg <= 1'b0;
199
+
200
+ if(cnt_eq_clk_n_per_bit_sub1) // 接收UART数据位
201
+ begin
202
+ cnt <= 0;
203
+ now_bit_i <= now_bit_i + 3'b001;
204
+
205
+ if(now_bit_i == 3'b111)
206
+ now_status <= status_stop;
207
+ else
208
+ now_status <= status_data;
209
+ end
210
+ else
211
+ begin
212
+ now_status <= status_data;
213
+ cnt <= cnt + 1;
214
+ end
215
+ end
216
+ status_stop: // 状态:停止位
217
+ begin
218
+ rx_idle_reg <= 1'b0;
219
+
220
+ if(cnt_eq_clk_n_per_bit_sub1) // 此时对齐到停止位的中央
221
+ begin
222
+ now_status <= status_idle;
223
+ cnt <= 0;
224
+ byte_vld <= 1'b1;
225
+ end
226
+ else
227
+ begin
228
+ now_status <= status_stop;
229
+ cnt <= cnt + 1;
230
+ end
231
+ end
232
+ default:
233
+ begin
234
+ now_status <= status_idle;
235
+ cnt <= 0;
236
+ now_bit_i <= 3'b000;
237
+ rx_idle_reg <= 1'b1;
238
+ end
239
+ endcase
240
+ end
241
+ end
242
+
243
+ // 当前接收的字节
244
+ always @(posedge clk)
245
+ begin
246
+ # simulation_delay;
247
+
248
+ if(now_status == status_data)
249
+ begin
250
+ case(now_bit_i)
251
+ 3'b000: byte[0] <= rx_stable;
252
+ 3'b001: byte[1] <= rx_stable;
253
+ 3'b010: byte[2] <= rx_stable;
254
+ 3'b011: byte[3] <= rx_stable;
255
+ 3'b100: byte[4] <= rx_stable;
256
+ 3'b101: byte[5] <= rx_stable;
257
+ 3'b110: byte[6] <= rx_stable;
258
+ 3'b111: byte[7] <= rx_stable;
259
+ endcase
260
+ end
261
+ end
262
+
263
+ endmodule
CutestPanda_Opensoc/101_apb_uart/rtl/uart_rx_tx.v ADDED
@@ -0,0 +1,138 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ MIT License
3
+
4
+ Copyright (c) 2024 Panda, 2257691535@qq.com
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
23
+ */
24
+
25
+ `timescale 1ns / 1ps
26
+ /********************************************************************
27
+ 本模块: UART控制器
28
+
29
+ 描述:
30
+ 使用发送/接收fifo的UART控制器
31
+ 可选AXIS接口或FIFO接口
32
+
33
+ 注意:
34
+ 当使用FIFO接口时, UART发送FIFO请使用标准FIFO
35
+ UART接收可能溢出
36
+
37
+ 协议:
38
+ AXIS MASTER/SLAVE
39
+ FIFO READ/WRITE
40
+ UART
41
+
42
+ 作者: 陈家耀
43
+ 日期: 2023/11/08
44
+ ********************************************************************/
45
+
46
+
47
+ module uart_rx_tx #(
48
+ parameter integer clk_frequency_MHz = 200, // 时钟频率
49
+ parameter integer baud_rate = 115200, // 波特率
50
+ parameter interface = "fifo", // 接口协议(axis|fifo)
51
+ parameter real simulation_delay = 1 // 仿真延时
52
+ )(
53
+ input wire clk,
54
+ input wire resetn,
55
+
56
+ input wire rx,
57
+ output wire tx,
58
+
59
+ output wire[7:0] m_axis_rx_byte_data,
60
+ output wire m_axis_rx_byte_valid,
61
+ input wire m_axis_rx_byte_ready,
62
+
63
+ output wire[7:0] rx_buf_fifo_din,
64
+ output wire rx_buf_fifo_wen,
65
+ input wire rx_buf_fifo_full,
66
+
67
+ input wire[7:0] s_axis_tx_byte_data,
68
+ input wire s_axis_tx_byte_valid,
69
+ output wire s_axis_tx_byte_ready,
70
+
71
+ input wire[7:0] tx_buf_fifo_dout,
72
+ input wire tx_buf_fifo_empty,
73
+ input wire tx_buf_fifo_almost_empty,
74
+ output wire tx_buf_fifo_ren,
75
+
76
+ output wire rx_err,
77
+ output wire tx_idle,
78
+ output wire rx_idle,
79
+ output wire tx_done,
80
+ output wire rx_done,
81
+ output wire rx_start
82
+ );
83
+
84
+ // 检查接收缓冲区是否溢出
85
+ reg rx_err_reg;
86
+
87
+ assign rx_err = rx_err_reg;
88
+
89
+ always @(posedge clk or negedge resetn)
90
+ begin
91
+ if(~resetn)
92
+ rx_err_reg <= 1'b0;
93
+ else
94
+ # simulation_delay rx_err_reg <= (interface == "axis") ? (m_axis_rx_byte_valid & (~m_axis_rx_byte_ready)):(rx_buf_fifo_wen & rx_buf_fifo_full);
95
+ end
96
+
97
+ /** uart收发 **/
98
+ localparam integer clk_frequency = clk_frequency_MHz * 1000000;
99
+
100
+ assign rx_buf_fifo_din = m_axis_rx_byte_data;
101
+ assign rx_buf_fifo_wen = m_axis_rx_byte_valid;
102
+
103
+ uart_rx #(
104
+ .clk_frequency(clk_frequency),
105
+ .baud_rate(baud_rate),
106
+ .simulation_delay(simulation_delay)
107
+ )uart_rx_u(
108
+ .clk(clk),
109
+ .rst_n(resetn),
110
+ .rx(rx),
111
+ .rx_byte_data(m_axis_rx_byte_data),
112
+ .rx_byte_valid(m_axis_rx_byte_valid),
113
+ .rx_byte_ready((interface == "axis") ? m_axis_rx_byte_ready:(~rx_buf_fifo_full)),
114
+ .rx_idle(rx_idle),
115
+ .rx_done(rx_done),
116
+ .rx_start(rx_start)
117
+ );
118
+
119
+ uart_tx #(
120
+ .clk_frequency(clk_frequency),
121
+ .baud_rate(baud_rate),
122
+ .interface(interface),
123
+ .simulation_delay(simulation_delay)
124
+ )uart_tx_u(
125
+ .clk(clk),
126
+ .rst_n(resetn),
127
+ .tx(tx),
128
+ .tx_byte_data(s_axis_tx_byte_data),
129
+ .tx_byte_valid(s_axis_tx_byte_valid),
130
+ .tx_byte_ready(s_axis_tx_byte_ready),
131
+ .tx_fifo_dout(tx_buf_fifo_dout),
132
+ .tx_fifo_empty(tx_buf_fifo_empty),
133
+ .tx_fifo_ren(tx_buf_fifo_ren),
134
+ .tx_idle(tx_idle),
135
+ .tx_done(tx_done)
136
+ );
137
+
138
+ endmodule
CutestPanda_Opensoc/101_apb_uart/rtl/uart_tx.v ADDED
@@ -0,0 +1,226 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ MIT License
3
+
4
+ Copyright (c) 2024 Panda, 2257691535@qq.com
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
23
+ */
24
+
25
+ `timescale 1ns / 1ps
26
+ /********************************************************************
27
+ 本模块: UART发送控制器
28
+
29
+ 描述:
30
+ UART发送控制器
31
+ 可选AXIS接口或FIFO接口
32
+
33
+ 注意:
34
+
35
+
36
+ 协议:
37
+ AXIS SLAVE
38
+ FIFO READ
39
+ UART
40
+
41
+ 作者: 陈家耀
42
+ 日期: 2023/11/08
43
+ ********************************************************************/
44
+
45
+
46
+ module uart_tx #(
47
+ parameter integer clk_frequency = 200000000, // 时钟频率
48
+ parameter integer baud_rate = 115200, // 波特率
49
+ parameter interface = "axis", // 接口协议(axis|fifo)
50
+ parameter real simulation_delay = 1 // 仿真延时
51
+ )(
52
+ input wire clk,
53
+ input wire rst_n,
54
+
55
+ output wire tx,
56
+
57
+ input wire[7:0] tx_byte_data,
58
+ input wire tx_byte_valid,
59
+ output wire tx_byte_ready,
60
+
61
+ input wire[7:0] tx_fifo_dout,
62
+ input wire tx_fifo_empty,
63
+ output wire tx_fifo_ren,
64
+
65
+ output wire tx_idle,
66
+ output wire tx_done
67
+ );
68
+
69
+ // 计算log2(bit_depth)
70
+ function integer clogb2 (input integer bit_depth);
71
+ integer temp;
72
+ begin
73
+ temp = bit_depth;
74
+ for(clogb2 = -1;temp > 0;clogb2 = clogb2 + 1)
75
+ temp = temp >> 1;
76
+ end
77
+ endfunction
78
+
79
+ localparam integer div_n = clk_frequency / baud_rate;
80
+
81
+ /** 发送fifo读端口 **/
82
+ wire[7:0] tx_fifo_data_out;
83
+ wire tx_fifo_empty_n;
84
+
85
+ reg ready_reg;
86
+
87
+ assign tx_byte_ready = ready_reg;
88
+ assign tx_fifo_data_out = (interface == "axis") ? tx_byte_data:tx_fifo_dout;
89
+ assign tx_fifo_empty_n = (interface == "axis") ? tx_byte_valid:(~tx_fifo_empty);
90
+
91
+ assign tx_fifo_ren = ready_reg;
92
+
93
+ /** 分频计数器 **/
94
+ reg[clogb2(div_n-1):0] cnt;
95
+ reg cnt_last_flag;
96
+
97
+ always @(posedge clk or negedge rst_n)
98
+ begin
99
+ if(~rst_n)
100
+ begin
101
+ cnt <= 0;
102
+ cnt_last_flag <= 1'b0;
103
+ end
104
+ else
105
+ begin
106
+ # simulation_delay;
107
+
108
+ if(cnt_last_flag)
109
+ cnt <= 0;
110
+ else
111
+ cnt <= cnt + 1;
112
+
113
+ cnt_last_flag <= cnt == div_n - 2;
114
+ end
115
+ end
116
+
117
+ /** 数据发送状态机 **/
118
+ localparam status_idle = 2'b00; // 状态:空闲
119
+ localparam status_start = 2'b01; // 状态:起始位
120
+ localparam status_data = 2'b10; // 状态:数据位
121
+ localparam status_stop = 2'b11; // 状态:停止位
122
+
123
+ reg[1:0] now_status; // 当前状态
124
+ reg[7:0] now_byte; // 待发送字节
125
+ reg tx_reg; // UART的tx端
126
+ reg[7:0] now_byte_i; // 当前发送字节的位编号(独一码)
127
+ reg byte_loaded; // 已加载待发送字节(标志)
128
+ reg tx_idle_reg; // UART控制器发送空闲(标志)
129
+ reg tx_done_reg; // UART控制器发送完成(标志)
130
+
131
+ assign tx = tx_reg;
132
+ assign tx_idle = tx_idle_reg;
133
+ assign tx_done = tx_done_reg;
134
+
135
+ always @(posedge clk or negedge rst_n)
136
+ begin
137
+ if(~rst_n)
138
+ begin
139
+ now_status <= status_idle;
140
+ tx_reg <= 1'b1;
141
+ now_byte_i <= 8'd1;
142
+ byte_loaded <= 1'b0;
143
+ ready_reg <= 1'b0;
144
+ tx_idle_reg <= 1'b1;
145
+ tx_done_reg <= 1'b0;
146
+ end
147
+ else
148
+ begin
149
+ # simulation_delay;
150
+
151
+ ready_reg <= 1'b0;
152
+ tx_done_reg <= 1'b0;
153
+
154
+ case(now_status)
155
+ status_idle: // 状态:空闲
156
+ begin
157
+ if(byte_loaded)
158
+ now_status <= status_start;
159
+
160
+ byte_loaded <= tx_fifo_empty_n & ready_reg;
161
+ tx_reg <= 1'b1;
162
+ ready_reg <= (~byte_loaded) & (~(tx_fifo_empty_n & ready_reg));
163
+ tx_idle_reg <= ~(byte_loaded | tx_fifo_empty_n); // 直接判断~tx_fifo_empty_n也可以???
164
+ end
165
+ status_start: // 状态:起始位
166
+ begin
167
+ if(cnt_last_flag)
168
+ begin
169
+ now_status <= status_data;
170
+ tx_reg <= 1'b0; // 产生下降沿 -> 起始位
171
+ end
172
+
173
+ byte_loaded <= 1'b0;
174
+ tx_idle_reg <= 1'b0;
175
+ end
176
+ status_data: // 状态:数据位
177
+ begin
178
+ if(cnt_last_flag)
179
+ begin
180
+ if(now_byte_i[7])
181
+ now_status <= status_stop;
182
+
183
+ now_byte_i <= {now_byte_i[6:0], now_byte_i[7]}; // 对当前发送字节的位编号(独一码)做循环左移
184
+ tx_reg <= now_byte[0]; // 发送下一数据位
185
+ end
186
+
187
+ byte_loaded <= 1'b0;
188
+ tx_idle_reg <= 1'b0;
189
+ end
190
+ status_stop: // 状态:停止位
191
+ begin
192
+ if(cnt_last_flag)
193
+ begin
194
+ now_status <= status_idle;
195
+ tx_reg <= 1'b1; // 产生停止位
196
+ tx_done_reg <= 1'b1;
197
+ end
198
+
199
+ byte_loaded <= 1'b0;
200
+ tx_idle_reg <= 1'b0;
201
+ end
202
+ default:
203
+ begin
204
+ now_status <= status_idle;
205
+ tx_reg <= 1'b1;
206
+ now_byte_i <= 8'd1;
207
+ byte_loaded <= 1'b0;
208
+ tx_idle_reg <= 1'b1;
209
+ end
210
+ endcase
211
+ end
212
+ end
213
+
214
+ // 待发送字节
215
+ always @(posedge clk)
216
+ begin
217
+ # simulation_delay;
218
+
219
+ if((now_status == status_idle) &
220
+ ((interface == "axis") ? (tx_fifo_empty_n & ready_reg):byte_loaded)) // 载入
221
+ now_byte <= tx_fifo_data_out;
222
+ else if((now_status == status_data) & cnt_last_flag) // 右移
223
+ now_byte <= {1'bx, now_byte[7:1]};
224
+ end
225
+
226
+ endmodule
CutestPanda_Opensoc/102_apb_i2c/firmware/apb_i2c.c ADDED
@@ -0,0 +1,187 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /************************************************************************************************************************
2
+ APB-I2C����(��Դ�ļ�)
3
+ @brief APB-I2C����
4
+ @date 2024/08/28
5
+ @author �¼�ҫ
6
+ @eidt 2024/08/28 1.00 �����˵�һ����ʽ�汾
7
+ ************************************************************************************************************************/
8
+
9
+ #include "apb_i2c.h"
10
+
11
+ ////////////////////////////////////////////////////////////////////////////////////////////////////////////
12
+
13
+ static int apb_i2c_tx(ApbI2C* i2c, uint8_t byte, uint8_t is_last); // ����FIFOд��һ���ֽ�����
14
+
15
+ ////////////////////////////////////////////////////////////////////////////////////////////////////////////
16
+
17
+ /*************************
18
+ @init
19
+ @public
20
+ @brief ��ʼ��APB-I2C
21
+ @param i2c APB-I2C(�ṹ��ָ��)
22
+ base_addr APB-I2C�������ַ
23
+ @return none
24
+ *************************/
25
+ void apb_i2c_init(ApbI2C* i2c, uint32_t base_addr){
26
+ i2c->hardware = (ApbI2CHd*)base_addr;
27
+ }
28
+
29
+ /*************************
30
+ @io
31
+ @public
32
+ @brief APB-I2C�������
33
+ @param i2c APB-I2C(�ṹ��ָ��)
34
+ slave_addr �ӻ���ַ
35
+ data �������ֽڻ�����(�׵�ַ)
36
+ len �������ֽ���
37
+ @attention ����fifoд���ݿ��ܻ��������
38
+ @return �Ƿ�ɹ�
39
+ *************************/
40
+ int apb_i2c_start_wt_trans(ApbI2C* i2c, uint8_t slave_addr, uint8_t* data, uint8_t len){
41
+ if(len > 15){ // ÿ��I2C���ݰ����ܳ���15�ֽ�
42
+ return -1;
43
+ }else{
44
+ while(apb_i2c_tx(i2c, slave_addr, 0));
45
+
46
+ for(uint8_t i = 0;i < len;i++){
47
+ while(apb_i2c_tx(i2c, data[i], i == (len - 1)));
48
+ }
49
+
50
+ return 0;
51
+ }
52
+ }
53
+
54
+ /*************************
55
+ @io
56
+ @public
57
+ @brief APB-I2C����������
58
+ @param i2c APB-I2C(�ṹ��ָ��)
59
+ slave_addr �ӻ���ַ
60
+ len �������ֽ���
61
+ @attention ����fifoд���ݿ��ܻ��������
62
+ @return �Ƿ�ɹ�
63
+ *************************/
64
+ int apb_i2c_start_rd_trans(ApbI2C* i2c, uint8_t slave_addr, uint8_t len){
65
+ if(len > 15){ // ÿ��I2C���ݰ����ܳ���15�ֽ�
66
+ return -1;
67
+ }else{
68
+ while(apb_i2c_tx(i2c, slave_addr | 0x01, 0));
69
+ while(apb_i2c_tx(i2c, len, 1));
70
+
71
+ return 0;
72
+ }
73
+ }
74
+
75
+ /*************************
76
+ @io
77
+ @public
78
+ @brief �ӽ���FIFO��ȡһ���ֽ�����
79
+ @param i2c APB-I2C(�ṹ��ָ��)
80
+ byte �����ֽڻ�����(�׵�ַ)
81
+ @return �Ƿ�ɹ�
82
+ *************************/
83
+ int apb_i2c_get_rx_byte(ApbI2C* i2c, uint8_t* byte){
84
+ uint32_t fifo_cs = i2c->hardware->fifo_cs;
85
+
86
+ if(fifo_cs & 0x00010000){
87
+ return -1;
88
+ }else{
89
+ i2c->hardware->fifo_cs = 0x00020000;
90
+
91
+ *byte = (uint8_t)(i2c->hardware->fifo_cs >> 18);
92
+
93
+ return 0;
94
+ }
95
+ }
96
+
97
+ /*************************
98
+ @cfg
99
+ @public
100
+ @brief APB-I2Cʹ���ж�
101
+ @param i2c APB-I2C(�ṹ��ָ��)
102
+ itr_en �ж�ʹ������
103
+ @return none
104
+ *************************/
105
+ void apb_i2c_enable_itr(ApbI2C* i2c, uint8_t itr_en){
106
+ i2c->hardware->itr_status_en = 0x00000001 | (((uint32_t)itr_en) << 8);
107
+ }
108
+
109
+ /*************************
110
+ @cfg
111
+ @public
112
+ @brief APB-I2C�����ж�
113
+ @param i2c APB-I2C(�ṹ��ָ��)
114
+ @return none
115
+ *************************/
116
+ void apb_i2c_disable_itr(ApbI2C* i2c){
117
+ i2c->hardware->itr_status_en = 0x00000000;
118
+ }
119
+
120
+ /*************************
121
+ @cfg
122
+ @public
123
+ @brief APB-I2C��������ʱ����
124
+ @param i2c APB-I2C(�ṹ��ָ��)
125
+ tx_bytes_n_th I2C�����ж��ֽ�����ֵ
126
+ rx_bytes_n_th I2C�����ж��ֽ�����ֵ
127
+ scl_div_n I2Cʱ�ӷ�Ƶϵ��
128
+ @return none
129
+ *************************/
130
+ void apb_i2c_config_params(ApbI2C* i2c, uint8_t tx_bytes_n_th, uint8_t rx_bytes_n_th, uint8_t scl_div_n){
131
+ i2c->hardware->itr_th = ((uint32_t)tx_bytes_n_th) | (((uint32_t)rx_bytes_n_th) << 8)
132
+ | (((uint32_t)scl_div_n) << 16);
133
+ }
134
+
135
+ /*************************
136
+ @sts
137
+ @public
138
+ @brief APB-I2C��ȡ�ж�״̬
139
+ @param i2c APB-I2C(�ṹ��ָ��)
140
+ tx_bytes_n I2C�����ֽ���(ָ��)
141
+ rx_bytes_n I2C�����ֽ���(ָ��)
142
+ @return �ж�״̬
143
+ *************************/
144
+ uint8_t apb_i2c_get_itr_status(ApbI2C* i2c, uint16_t* tx_bytes_n, uint16_t* rx_bytes_n){
145
+ uint8_t itr_status = ((uint8_t)(i2c->hardware->itr_flag_status >> 1)) & 0x0000000F;
146
+
147
+ uint32_t rx_tx_bytes_n = (i2c->hardware->itr_flag_status >> 8);
148
+
149
+ *tx_bytes_n = (uint16_t)(rx_tx_bytes_n & 0x00000FFF);
150
+ *rx_bytes_n = (uint16_t)(rx_tx_bytes_n >> 12);
151
+
152
+ return itr_status;
153
+ }
154
+
155
+ /*************************
156
+ @sts
157
+ @public
158
+ @brief APB-I2C����жϱ�־
159
+ @param i2c APB-I2C(�ṹ��ָ��)
160
+ @return none
161
+ *************************/
162
+ void apb_i2c_clear_itr_flag(ApbI2C* i2c){
163
+ i2c->hardware->itr_flag_status = 0x00000000;
164
+ }
165
+
166
+ ////////////////////////////////////////////////////////////////////////////////////////////////////////////
167
+
168
+ /*************************
169
+ @io
170
+ @private
171
+ @brief ����FIFOд��һ���ֽ�����
172
+ @param i2c APB-I2C(�ṹ��ָ��)
173
+ byte ��д����ֽ�����
174
+ is_last ������lastָʾ
175
+ @return �Ƿ�ɹ�
176
+ *************************/
177
+ static int apb_i2c_tx(ApbI2C* i2c, uint8_t byte, uint8_t is_last){
178
+ uint32_t fifo_cs = i2c->hardware->fifo_cs;
179
+
180
+ if(fifo_cs & 0x00000001){
181
+ return -1;
182
+ }else{
183
+ i2c->hardware->fifo_cs = 0x00000002 | (((uint32_t)byte) << 2) | (((uint32_t)is_last) << 10);
184
+
185
+ return 0;
186
+ }
187
+ }
CutestPanda_Opensoc/102_apb_i2c/firmware/apb_i2c.h ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #include <stdint.h>
2
+
3
+ ////////////////////////////////////////////////////////////////////////////////////////////////////////////
4
+
5
+ // ���������
6
+ #define I2C_TX_BYTES_N_ITR_MASK 0x01 // I2C����ָ���ֽ����жϱ�־
7
+ #define I2C_SLAVE_RESP_ERR_ITR_MASK 0x02 // I2C�ӻ���Ӧ�����жϱ�־
8
+ #define I2C_RX_BYTES_N_ITR_MASK 0x04 // I2C����ָ���ֽ����жϱ�־
9
+ #define I2C_RX_OVF_ITR_MASK 0x08 // I2C��������жϱ�־
10
+
11
+ ////////////////////////////////////////////////////////////////////////////////////////////////////////////
12
+
13
+ #ifndef __APB_I2C_H
14
+ #define __APB_I2C_H
15
+
16
+ typedef struct{
17
+ uint32_t fifo_cs; // �շ�fifo����
18
+ uint32_t itr_status_en; // �ж�ʹ��
19
+ uint32_t itr_th; // ����ʱ����
20
+ uint32_t itr_flag_status; // �жϱ�־��״̬��Ϣ
21
+ }ApbI2CHd;
22
+
23
+ typedef struct{
24
+ ApbI2CHd* hardware; // APB-I2C�Ĵ����ӿ�(�ṹ��ָ��)
25
+ }ApbI2C;
26
+
27
+ #endif
28
+
29
+ ////////////////////////////////////////////////////////////////////////////////////////////////////////////
30
+
31
+ void apb_i2c_init(ApbI2C* i2c, uint32_t base_addr); // ��ʼ��APB-I2C
32
+
33
+ int apb_i2c_start_wt_trans(ApbI2C* i2c, uint8_t slave_addr, uint8_t* data, uint8_t len); // APB-I2C�������
34
+ int apb_i2c_start_rd_trans(ApbI2C* i2c, uint8_t slave_addr, uint8_t len); // APB-I2C����������
35
+
36
+ int apb_i2c_get_rx_byte(ApbI2C* i2c, uint8_t* byte); // �ӽ���FIFO��ȡһ���ֽ�����
37
+
38
+ // APB-I2C��������ʱ����
39
+ void apb_i2c_config_params(ApbI2C* i2c, uint8_t tx_bytes_n_th, uint8_t rx_bytes_n_th, uint8_t scl_div_n);
40
+
41
+ void apb_i2c_enable_itr(ApbI2C* i2c, uint8_t itr_en); // APB-I2Cʹ���ж�
42
+ void apb_i2c_disable_itr(ApbI2C* i2c); // APB-I2C�����ж�
43
+ uint8_t apb_i2c_get_itr_status(ApbI2C* i2c, uint16_t* tx_bytes_n, uint16_t* rx_bytes_n); // APB-I2C��ȡ�ж�״̬
44
+ void apb_i2c_clear_itr_flag(ApbI2C* i2c); // APB-I2C����жϱ�־
CutestPanda_Opensoc/102_apb_i2c/firmware/examples/apb_i2c_eeprom_example.c ADDED
@@ -0,0 +1,101 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /************************************************************************************************************************
2
+ APB-I2Cʾ������
3
+ @brief ����APB-I2Cʵ��EEPROM��д
4
+ @attention �����Ӳ��ƽ̨�������ӳ�(delay)��ص�API
5
+ @date 2024/08/28
6
+ @author �¼�ҫ
7
+ @eidt 2024/08/28 1.00 �����˵�һ����ʽ�汾
8
+ ************************************************************************************************************************/
9
+
10
+ #include "../apb_i2c.h"
11
+ #include "delay.h"
12
+
13
+ ////////////////////////////////////////////////////////////////////////////////////////////////////////////
14
+
15
+ #define APB_I2C_BASEADDR 0x40000000 // APB-I2C�������ַ
16
+
17
+ #define EEPROM_I2C_ADDR 0xA0 // EEPROM I2C�ӻ���ַ
18
+
19
+ ////////////////////////////////////////////////////////////////////////////////////////////////////////////
20
+
21
+ static ApbI2C i2c; // APB-I2C����ṹ��
22
+
23
+ ////////////////////////////////////////////////////////////////////////////////////////////////////////////
24
+
25
+ static uint8_t read_eeprom_by_i2c(ApbI2C* i2c, uint8_t slave_addr, uint16_t mem_addr); // ��EEPROM
26
+ static void write_eeprom_by_i2c(ApbI2C* i2c, uint8_t slave_addr, uint16_t mem_addr, uint8_t data); // дEEPROM
27
+
28
+ ////////////////////////////////////////////////////////////////////////////////////////////////////////////
29
+
30
+ /*************************
31
+ @io
32
+ @public
33
+ @brief ��EEPROM
34
+ @param i2c APB-I2C(�ṹ��ָ��)
35
+ slave_addr I2C�ӻ���ַ
36
+ mem_addr ��ȡ�ĵ�ַ
37
+ @return ��ȡ�����ֽ�����
38
+ *************************/
39
+ static uint8_t read_eeprom_by_i2c(ApbI2C* i2c, uint8_t slave_addr, uint16_t mem_addr){
40
+ uint8_t recv_buf;
41
+
42
+ uint8_t send_buf[2] = {(uint8_t)(mem_addr >> 8), (uint8_t)(mem_addr & 0x00FF)};
43
+
44
+ apb_i2c_start_wt_trans(i2c, slave_addr, send_buf, 2); // �������
45
+ apb_i2c_start_rd_trans(i2c, slave_addr, 1); // ����������
46
+
47
+ while(!apb_i2c_get_rx_byte(i2c, &recv_buf)); // ��ȡ1���ֽ�
48
+
49
+ return recv_buf;
50
+ }
51
+
52
+ /*************************
53
+ @io
54
+ @public
55
+ @brief дEEPROM
56
+ @param i2c APB-I2C(�ṹ��ָ��)
57
+ slave_addr I2C�ӻ���ַ
58
+ mem_addr д��ĵ�ַ
59
+ data ��д����ֽ�����
60
+ @return none
61
+ *************************/
62
+ static void write_eeprom_by_i2c(ApbI2C* i2c, uint8_t slave_addr, uint16_t mem_addr, uint8_t data){
63
+ uint8_t send_buf[3] = {(uint8_t)(mem_addr >> 8), (uint8_t)(mem_addr & 0x00FF), data};
64
+
65
+ apb_i2c_start_wt_trans(i2c, slave_addr, send_buf, 3); // �������
66
+
67
+ delay_ms(10); // ��ʱ0.01s
68
+ }
69
+
70
+ ////////////////////////////////////////////////////////////////////////////////////////////////////////////
71
+
72
+ void apb_i2c_eeprom_example(void){
73
+ // ��ʼ��APB-I2C
74
+ apb_i2c_init(&i2c, APB_I2C_BASEADDR);
75
+
76
+ // ��������ʱ����
77
+ // I2Cʱ�ӷ�Ƶϵ�� = 7
78
+ apb_i2c_config_params(&i2c, 1, 1, 7);
79
+
80
+ // ���ַ0x00д���ֽ�0x0A
81
+ write_eeprom_by_i2c(&i2c, EEPROM_I2C_ADDR, 0x00, 0x0A);
82
+
83
+ // ���ַ0x01д���ֽ�0xFE
84
+ write_eeprom_by_i2c(&i2c, EEPROM_I2C_ADDR, 0x01, 0xFE);
85
+
86
+ // ��ȡ��ַ0x00��0x01
87
+ uint8_t rdata[2];
88
+
89
+ rdata[0] = read_eeprom_by_i2c(&i2c, EEPROM_I2C_ADDR, 0x00);
90
+ rdata[1] = read_eeprom_by_i2c(&i2c, EEPROM_I2C_ADDR, 0x01);
91
+
92
+ if((rdata[0] == 0x0A) && (rdata[1] == 0xFE)){
93
+ // ��д��֤�ɹ�
94
+ // ...
95
+ }else{
96
+ // ��д��֤ʧ��
97
+ // ...
98
+ }
99
+
100
+ while(1);
101
+ }
CutestPanda_Opensoc/102_apb_i2c/firmware/examples/delay.c ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #include "delay.h"
2
+
3
+ ////////////////////////////////////////////////////////////////////////////////////////////////////////////
4
+
5
+ static uint32_t cnt = 0;
6
+
7
+ ////////////////////////////////////////////////////////////////////////////////////////////////////////////
8
+
9
+ void SysTick_Handler(void){
10
+ cnt++;
11
+ }
12
+
13
+ void delay_ms(uint32_t t){
14
+ cnt = 0;
15
+
16
+ while(cnt != t);
17
+ }
CutestPanda_Opensoc/102_apb_i2c/firmware/examples/delay.h ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ #include <stdint.h>
2
+
3
+ ////////////////////////////////////////////////////////////////////////////////////////////////////////////
4
+
5
+ void delay_ms(uint32_t t);
CutestPanda_Opensoc/102_apb_i2c/rtl/apb_i2c.v ADDED
@@ -0,0 +1,285 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ MIT License
3
+
4
+ Copyright (c) 2024 Panda, 2257691535@qq.com
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
23
+ */
24
+
25
+ `timescale 1ns / 1ps
26
+ /********************************************************************
27
+ 本模块: 符合APB协议的I2C控制器
28
+
29
+ 描述:
30
+ APB-I2C控制器
31
+ 支持I2C收发中断
32
+ 支持7位/10位地址
33
+
34
+ 发送fifo数据包格式:
35
+ 写数据 -> last0 xxxx_xxx0 地址阶段
36
+ [last0 xxxx_xxxx 地址阶段(仅10位地址时需要)]
37
+ last0 xxxx_xxxx 数据阶段
38
+ ...
39
+ last1 xxxx_xxxx 数据阶段
40
+ 读数据 -> last0 xxxx_xxx1 地址阶段
41
+ [last0 xxxx_xxxx 地址阶段(仅10位地址时需要)]
42
+ last1 8位待读取字节数
43
+
44
+ 寄存器->
45
+ 偏移量 | 含义 | 读写特性 | 备注
46
+ 0x00 0:发送fifo是否满 R
47
+ 1:发送fifo写使能 W 写该寄存器且该位为1'b1时产生发送fifo写使能
48
+ 10~2:发送fifo写数据 W {last(1bit), data(8bit)}
49
+ 16:接收fifo是否空 R
50
+ 17:接收fifo读使能 W 写该寄存器且该位为1'b1时产生接收fifo读使能
51
+ 25~18:接收fifo读数据 R
52
+ 0x04 0:I2C全局中断使能 W
53
+ 8:I2C发送指定字节数中断使能 W
54
+ 9:I2C从机响应错误中断使能 W
55
+ 10:I2C接收指定字节数中断使能 W
56
+ 11:I2C接收溢出中断使能 W
57
+ 0x08 7~0:I2C发送中断字节数阈值 W 发送字节数 > 阈值时发生中断
58
+ 15~8:I2C接收中断字节数阈值 W 接收字节数 > 阈值时发生中断
59
+ 23~16:I2C时钟分频系数 W 分频数 = (分频系数 + 1) * 2
60
+ 分频系数应>=1
61
+ 0x0C 0:I2C全局中断标志 RWC 请在中断服务函数中清除中断标志
62
+ 1:I2C发送指定字节数中断标志 R
63
+ 2:I2C从机响应错误中断标志 R
64
+ 3:I2C接收指定字节数中断标志 R
65
+ 4:I2C接收溢出中断标志 R
66
+ 19~8:I2C发送字节数 RWC 每当发送一个I2C数据包后更新
67
+ 31~20:I2C接收字节数 RWC 每当接收一个I2C数据包后更新
68
+
69
+ 注意:
70
+ I2C发送/接收指定字节数中断每当发送/接收一个I2C数据包后判断
71
+ 每个I2C数据包不能超过15字节
72
+
73
+ 协议:
74
+ APB SLAVE
75
+ I2C MASTER
76
+
77
+ 作者: 陈家耀
78
+ 日期: 2024/06/14
79
+ ********************************************************************/
80
+
81
+
82
+ module apb_i2c #(
83
+ parameter integer addr_bits_n = 7, // 地址位数(7|10)
84
+ parameter en_i2c_rx = "true", // 是否使能i2c接收
85
+ parameter tx_rx_fifo_ram_type = "bram", // 发送接收fifo的RAM类型(lutram|bram)
86
+ parameter integer tx_fifo_depth = 1024, // 发送fifo深度(32|64|128|...)
87
+ parameter integer rx_fifo_depth = 1024, // 接收fifo深度(32|64|128|...)
88
+ parameter real simulation_delay = 1 // 仿真延时
89
+ )(
90
+ // 时钟和复位
91
+ input wire clk,
92
+ input wire resetn,
93
+
94
+ // APB从机接口
95
+ input wire[31:0] paddr,
96
+ input wire psel,
97
+ input wire penable,
98
+ input wire pwrite,
99
+ input wire[31:0] pwdata,
100
+ output wire pready_out, // const -> 1'b1
101
+ output wire[31:0] prdata_out,
102
+ output wire pslverr_out, // const -> 1'b0
103
+
104
+ // I2C主机接口
105
+ // scl
106
+ output wire scl_t, // 1'b1为输入, 1'b0为输出
107
+ input wire scl_i,
108
+ output wire scl_o,
109
+ // sda
110
+ output wire sda_t, // 1'b1为输入, 1'b0为输出
111
+ input wire sda_i,
112
+ output wire sda_o,
113
+
114
+ // 中断信号
115
+ output wire itr
116
+ );
117
+
118
+ /** 收发fifo **/
119
+ // 发送fifo写端口
120
+ wire[8:0] tx_fifo_din;
121
+ wire tx_fifo_wen;
122
+ wire tx_fifo_full;
123
+ // 发送fifo读端口
124
+ wire tx_fifo_ren;
125
+ wire tx_fifo_empty;
126
+ wire[7:0] tx_fifo_dout;
127
+ wire tx_fifo_dout_last;
128
+ // 接收fifo写端口
129
+ wire rx_fifo_wen;
130
+ wire rx_fifo_full;
131
+ wire[7:0] rx_fifo_din;
132
+ // 接收fifo读端口
133
+ wire rx_fifo_ren;
134
+ wire rx_fifo_empty;
135
+ wire[7:0] rx_fifo_dout;
136
+
137
+ // 发送fifo
138
+ ram_fifo_wrapper #(
139
+ .fwft_mode("false"),
140
+ .ram_type(tx_rx_fifo_ram_type),
141
+ .en_bram_reg("false"),
142
+ .fifo_depth(tx_fifo_depth),
143
+ .fifo_data_width(9),
144
+ .full_assert_polarity("high"),
145
+ .empty_assert_polarity("high"),
146
+ .almost_full_assert_polarity("no"),
147
+ .almost_empty_assert_polarity("no"),
148
+ .en_data_cnt("false"),
149
+ .almost_full_th(),
150
+ .almost_empty_th(),
151
+ .simulation_delay(simulation_delay)
152
+ )tx_fifo(
153
+ .clk(clk),
154
+ .rst_n(resetn),
155
+ .fifo_wen(tx_fifo_wen),
156
+ .fifo_din(tx_fifo_din),
157
+ .fifo_full(tx_fifo_full),
158
+ .fifo_ren(tx_fifo_ren),
159
+ .fifo_dout({tx_fifo_dout_last, tx_fifo_dout}),
160
+ .fifo_empty(tx_fifo_empty)
161
+ );
162
+
163
+ // 接收fifo
164
+ generate
165
+ if(en_i2c_rx == "true")
166
+ begin
167
+ ram_fifo_wrapper #(
168
+ .fwft_mode("false"),
169
+ .ram_type(tx_rx_fifo_ram_type),
170
+ .en_bram_reg("false"),
171
+ .fifo_depth(rx_fifo_depth),
172
+ .fifo_data_width(8),
173
+ .full_assert_polarity("high"),
174
+ .empty_assert_polarity("high"),
175
+ .almost_full_assert_polarity("no"),
176
+ .almost_empty_assert_polarity("no"),
177
+ .en_data_cnt("false"),
178
+ .almost_full_th(),
179
+ .almost_empty_th(),
180
+ .simulation_delay(simulation_delay)
181
+ )rx_fifo(
182
+ .clk(clk),
183
+ .rst_n(resetn),
184
+ .fifo_wen(rx_fifo_wen),
185
+ .fifo_din(rx_fifo_din),
186
+ .fifo_full(rx_fifo_full),
187
+ .fifo_ren(rx_fifo_ren),
188
+ .fifo_dout(rx_fifo_dout),
189
+ .fifo_empty(rx_fifo_empty)
190
+ );
191
+ end
192
+ else
193
+ begin
194
+ assign rx_fifo_full = 1'b0;
195
+
196
+ assign rx_fifo_dout = 8'dx;
197
+ assign rx_fifo_empty = 1'b1;
198
+ end
199
+ endgenerate
200
+
201
+ /** 寄存器接口和中断处理 **/
202
+ // I2C时钟分频系数
203
+ wire[7:0] i2c_scl_div_rate;
204
+ // I2C发送完成指示
205
+ wire i2c_tx_done;
206
+ wire[3:0] i2c_tx_bytes_n;
207
+ // I2C接收完成指示
208
+ wire i2c_rx_done;
209
+ wire[3:0] i2c_rx_bytes_n;
210
+ // I2C从机响应错误
211
+ wire i2c_slave_resp_err;
212
+ // I2C接收溢出
213
+ wire i2c_rx_overflow;
214
+
215
+ // 寄存器接口和中断处理
216
+ regs_if_for_i2c #(
217
+ .simulation_delay(simulation_delay)
218
+ )regs_if_for_i2c_u(
219
+ .clk(clk),
220
+ .resetn(resetn),
221
+
222
+ .paddr(paddr),
223
+ .psel(psel),
224
+ .penable(penable),
225
+ .pwrite(pwrite),
226
+ .pwdata(pwdata),
227
+ .pready_out(pready_out),
228
+ .prdata_out(prdata_out),
229
+ .pslverr_out(pslverr_out),
230
+
231
+ .tx_fifo_wen(tx_fifo_wen),
232
+ .tx_fifo_din(tx_fifo_din),
233
+ .tx_fifo_full(tx_fifo_full),
234
+
235
+ .rx_fifo_ren(rx_fifo_ren),
236
+ .rx_fifo_dout(rx_fifo_dout),
237
+ .rx_fifo_empty(rx_fifo_empty),
238
+
239
+ .i2c_scl_div_rate(i2c_scl_div_rate),
240
+
241
+ .i2c_tx_done(i2c_tx_done),
242
+ .i2c_tx_bytes_n(i2c_tx_bytes_n),
243
+ .i2c_rx_done(i2c_rx_done),
244
+ .i2c_rx_bytes_n(i2c_rx_bytes_n),
245
+ .i2c_slave_resp_err(i2c_slave_resp_err),
246
+ .i2c_rx_overflow(i2c_rx_overflow),
247
+
248
+ .itr(itr)
249
+ );
250
+
251
+ /** I2C控制器 **/
252
+ i2c_ctrler #(
253
+ .addr_bits_n(addr_bits_n),
254
+ .simulation_delay(simulation_delay)
255
+ )i2c_ctrler_u(
256
+ .clk(clk),
257
+ .resetn(resetn),
258
+
259
+ .i2c_scl_div_rate(i2c_scl_div_rate),
260
+
261
+ .tx_fifo_ren(tx_fifo_ren),
262
+ .tx_fifo_empty(tx_fifo_empty),
263
+ .tx_fifo_dout(tx_fifo_dout),
264
+ .tx_fifo_dout_last(tx_fifo_dout_last),
265
+
266
+ .rx_fifo_wen(rx_fifo_wen),
267
+ .rx_fifo_full(rx_fifo_full),
268
+ .rx_fifo_din(rx_fifo_din),
269
+
270
+ .i2c_tx_done(i2c_tx_done),
271
+ .i2c_tx_bytes_n(i2c_tx_bytes_n),
272
+ .i2c_rx_done(i2c_rx_done),
273
+ .i2c_rx_bytes_n(i2c_rx_bytes_n),
274
+ .i2c_slave_resp_err(i2c_slave_resp_err),
275
+ .i2c_rx_overflow(i2c_rx_overflow),
276
+
277
+ .scl_t(scl_t),
278
+ .scl_i(scl_i),
279
+ .scl_o(scl_o),
280
+ .sda_t(sda_t),
281
+ .sda_i(sda_i),
282
+ .sda_o(sda_o)
283
+ );
284
+
285
+ endmodule
CutestPanda_Opensoc/102_apb_i2c/rtl/bram_simple_dual_port.v ADDED
@@ -0,0 +1,132 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ MIT License
3
+
4
+ Copyright (c) 2024 Panda, 2257691535@qq.com
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
23
+ */
24
+
25
+ `timescale 1ns / 1ps
26
+ /********************************************************************
27
+ 本模块: 简单双端口Bram
28
+
29
+ 描述:
30
+ 可选读延迟1clk或2clk
31
+
32
+ 注意:
33
+
34
+
35
+ 协议:
36
+ MEM READ/WRITE
37
+
38
+ 作者: 陈家耀
39
+ 日期: 2022/1/11
40
+ ********************************************************************/
41
+
42
+
43
+ module bram_simple_dual_port #(
44
+ parameter style = "HIGH_PERFORMANCE", // 存储器样式(HIGH_PERFORMANCE|LOW_LATENCY)
45
+ parameter integer mem_width = 32, // 存储器位宽
46
+ parameter integer mem_depth = 4096, // 存储器深度
47
+ parameter INIT_FILE = "no_init", // 初始化文件路径
48
+ parameter integer simulation_delay = 1 // 仿真延时
49
+ )
50
+ (
51
+ // clk
52
+ input wire clk,
53
+
54
+ // mem write
55
+ input wire wen_a,
56
+ input wire[clogb2(mem_depth-1):0] addr_a,
57
+ input wire[mem_width-1:0] din_a,
58
+
59
+ // mem read
60
+ input wire ren_b,
61
+ input wire[clogb2(mem_depth-1):0] addr_b,
62
+ output wire[mem_width-1:0] dout_b
63
+ );
64
+
65
+ // 计算bit_depth的最高有效位编号(即位数-1)
66
+ function integer clogb2 (input integer bit_depth);
67
+ begin
68
+ for(clogb2=-1; bit_depth>0; clogb2=clogb2+1)
69
+ bit_depth = bit_depth >> 1;
70
+ end
71
+ endfunction
72
+
73
+ (* ram_style="block" *) reg[mem_width-1:0] mem[mem_depth-1:0]; // 存储器
74
+ reg[mem_width-1:0] ram_data_b;
75
+
76
+ generate
77
+ if (INIT_FILE != "")
78
+ begin
79
+ if(INIT_FILE == "default")
80
+ begin
81
+ integer ram_index;
82
+ initial
83
+ for (ram_index = 0; ram_index < mem_depth; ram_index = ram_index + 1)
84
+ mem[ram_index] = ram_index;
85
+ end
86
+ else if(INIT_FILE != "no_init")
87
+ begin
88
+ initial
89
+ $readmemh(INIT_FILE, mem, 0, mem_depth - 1);
90
+ end
91
+ end
92
+ else
93
+ begin
94
+ integer ram_index;
95
+ initial
96
+ for (ram_index = 0; ram_index < mem_depth; ram_index = ram_index + 1)
97
+ mem[ram_index] = {mem_width{1'b0}};
98
+ end
99
+ endgenerate
100
+
101
+ // 读写控制逻辑
102
+ always @(posedge clk)
103
+ begin
104
+ if(wen_a)
105
+ # simulation_delay mem[addr_a] <= din_a;
106
+ end
107
+
108
+ always @(posedge clk)
109
+ begin
110
+ if(ren_b)
111
+ # simulation_delay ram_data_b <= mem[addr_b];
112
+ end
113
+
114
+ generate
115
+ if(style == "HIGH_PERFORMANCE")
116
+ begin
117
+ // 使用输出寄存器
118
+ reg[mem_width-1:0] data_b;
119
+
120
+ assign dout_b = data_b;
121
+
122
+ always @(posedge clk)
123
+ #simulation_delay data_b <= ram_data_b;
124
+ end
125
+ else
126
+ begin
127
+ // 不使用输出寄存器
128
+ assign dout_b = ram_data_b;
129
+ end
130
+ endgenerate
131
+
132
+ endmodule
CutestPanda_Opensoc/102_apb_i2c/rtl/dram_simple_dual_port.v ADDED
@@ -0,0 +1,129 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ MIT License
3
+
4
+ Copyright (c) 2024 Panda, 2257691535@qq.com
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
23
+ */
24
+
25
+ `timescale 1ns / 1ps
26
+ //////////////////////////////////////////////////////////////////////////////////
27
+ // 项目: 简单双端口Dram
28
+ /*
29
+ 描述:
30
+ 可选的输出寄存器
31
+ */
32
+ /*
33
+ 注意:
34
+
35
+ */
36
+ // 作者: 陈家耀
37
+ // 日期: 2022/1/13
38
+ //////////////////////////////////////////////////////////////////////////////////
39
+
40
+
41
+ module dram_simple_dual_port #(
42
+ parameter integer mem_width = 24, // 存储器位宽
43
+ parameter integer mem_depth = 32, // 存储器深度
44
+ parameter INIT_FILE = "no_init", // 初始化文件路径
45
+ parameter use_output_register = "true", // 是否使用输出寄存器
46
+ parameter output_register_init_v = 0, // 输出寄存器复位值
47
+ parameter real simulation_delay = 10 // 仿真时用的延时
48
+ )(
49
+ // 时钟和复位
50
+ input wire clk,
51
+ input wire rst_n,
52
+
53
+ // mem
54
+ // write port
55
+ input wire wen_a,
56
+ input wire[clogb2(mem_depth-1):0] addr_a,
57
+ input wire[mem_width-1:0] din_a,
58
+ // read port
59
+ input wire ren_b,
60
+ input wire[clogb2(mem_depth-1):0] addr_b,
61
+ output wire[mem_width-1:0] dout_b
62
+ );
63
+
64
+ // 计算bit_depth的最高有效位编号(即位数-1)
65
+ function integer clogb2 (input integer bit_depth);
66
+ integer temp;
67
+ begin
68
+ temp = bit_depth;
69
+ for(clogb2 = -1;temp > 0;clogb2 = clogb2 + 1)
70
+ temp = temp >> 1;
71
+ end
72
+ endfunction
73
+
74
+ (* ram_style="distributed" *) reg[mem_width-1:0] mem[mem_depth-1:0]; // 存储器
75
+
76
+ generate
77
+ if (INIT_FILE != "")
78
+ begin
79
+ if(INIT_FILE != "no_init")
80
+ begin
81
+ initial
82
+ $readmemh(INIT_FILE, mem, 0, mem_depth - 1);
83
+ end
84
+ end
85
+ else
86
+ begin
87
+ integer ram_index;
88
+ initial
89
+ for (ram_index = 0; ram_index < mem_depth; ram_index = ram_index + 1)
90
+ mem[ram_index] = {mem_width{1'b0}};
91
+ end
92
+ endgenerate
93
+
94
+ // 读写控制逻辑
95
+ generate
96
+ if(use_output_register == "true")
97
+ begin
98
+ reg[mem_width-1:0] dout_b_regs;
99
+
100
+ assign dout_b = dout_b_regs;
101
+
102
+ always @(posedge clk)
103
+ begin
104
+ if(wen_a)
105
+ # simulation_delay mem[addr_a] <= din_a;
106
+ end
107
+
108
+ always @(posedge clk or negedge rst_n)
109
+ begin
110
+ if(~rst_n)
111
+ dout_b_regs <= output_register_init_v;
112
+ else if(ren_b)
113
+ # simulation_delay dout_b_regs <= mem[addr_b];
114
+ end
115
+ end
116
+ else
117
+ begin
118
+ always @(posedge clk)
119
+ begin
120
+ # simulation_delay;
121
+ if(wen_a)
122
+ mem[addr_a] <= din_a;
123
+ end
124
+
125
+ assign dout_b = mem[addr_b];
126
+ end
127
+ endgenerate
128
+
129
+ endmodule
CutestPanda_Opensoc/102_apb_i2c/rtl/fifo_base_on_ram.v ADDED
@@ -0,0 +1,200 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ MIT License
3
+
4
+ Copyright (c) 2024 Panda, 2257691535@qq.com
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
23
+ */
24
+
25
+ `timescale 1ns / 1ps
26
+ /********************************************************************
27
+ 本模块: 基于ram的同步fifo控制器
28
+
29
+ 描述:
30
+ 全流水的高性能同步fifo控制器
31
+ 基于ram
32
+ 支持first word fall through特性(READ LA = 0)
33
+ 可选的固定阈值将满/将空信号
34
+
35
+ 注意:
36
+ 将满信号当存储计数 >= almost_full_th时有效
37
+ 将空信号当存储计数 <= almost_empty_th时有效
38
+ almost_full_th和almost_empty_th必须在[1, fifo_depth-1]范围内
39
+ 不使能FWFT时, 要求ram的读延迟=1或2clk; 使能FWFT时, 要求ram的读延迟=1clk
40
+
41
+ 协议:
42
+ FIFO WRITE/READ
43
+ MEM WRITE/READ
44
+
45
+ 作者: 陈家耀
46
+ 日期: 2023/10/29
47
+ ********************************************************************/
48
+
49
+
50
+ module fifo_based_on_ram #(
51
+ parameter fwft_mode = "true", // 是否启用first word fall through特性
52
+ parameter ram_read_la = 1, // ram读延迟(1|2)(仅在不使能FWFT时可用)
53
+ parameter integer fifo_depth = 32, // fifo深度(必须为2|4|8|16|...)
54
+ parameter integer fifo_data_width = 32, // fifo位宽
55
+ parameter integer almost_full_th = 20, // fifo将满阈值
56
+ parameter integer almost_empty_th = 5, // fifo将空阈值
57
+ parameter real simulation_delay = 1 // 仿真延时
58
+ )(
59
+ // 时钟和复位
60
+ input wire clk,
61
+ input wire rst_n,
62
+
63
+ // FIFO WRITE(fifo写端口)
64
+ input wire fifo_wen,
65
+ input wire[fifo_data_width-1:0] fifo_din,
66
+ output wire fifo_full,
67
+ output wire fifo_full_n,
68
+ output wire fifo_almost_full,
69
+ output wire fifo_almost_full_n,
70
+
71
+ // FIFO READ(fifo读端口)
72
+ input wire fifo_ren,
73
+ output wire[fifo_data_width-1:0] fifo_dout,
74
+ output wire fifo_empty,
75
+ output wire fifo_empty_n,
76
+ output wire fifo_almost_empty,
77
+ output wire fifo_almost_empty_n,
78
+
79
+ // MEM WRITE(ram写端口)
80
+ output wire ram_wen,
81
+ output wire[clogb2(fifo_depth-1):0] ram_w_addr,
82
+ output wire[fifo_data_width-1:0] ram_din,
83
+
84
+ // MEM RAD(ram读端口)
85
+ output wire ram_ren,
86
+ output wire[clogb2(fifo_depth-1):0] ram_r_addr,
87
+ input wire[fifo_data_width-1:0] ram_dout,
88
+
89
+ // 存储计数
90
+ output wire[clogb2(fifo_depth):0] data_cnt
91
+ );
92
+
93
+ // 计算log2(bit_depth)
94
+ function integer clogb2 (input integer bit_depth);
95
+ integer temp;
96
+ begin
97
+ temp = bit_depth;
98
+ for(clogb2 = -1;temp > 0;clogb2 = clogb2 + 1)
99
+ temp = temp >> 1;
100
+ end
101
+ endfunction
102
+
103
+ /** 主fifo **/
104
+ wire m_fifo_ren;
105
+ wire[fifo_data_width-1:0] m_fifo_dout;
106
+ wire m_fifo_empty_n;
107
+
108
+ generate
109
+ if(fwft_mode == "true")
110
+ begin
111
+ // FWFT模式
112
+ fifo_based_on_ram_std #(
113
+ .fifo_depth(fifo_depth),
114
+ .fifo_data_width(fifo_data_width),
115
+ .almost_full_th(almost_full_th),
116
+ .almost_empty_th(almost_empty_th),
117
+ .simulation_delay(simulation_delay)
118
+ )fifo_based_on_ram_std_u(
119
+ .clk(clk),
120
+ .rst_n(rst_n),
121
+ .fifo_wen(fifo_wen),
122
+ .fifo_din(fifo_din),
123
+ .fifo_full(fifo_full),
124
+ .fifo_full_n(fifo_full_n),
125
+ .fifo_almost_full(fifo_almost_full),
126
+ .fifo_almost_full_n(fifo_almost_full_n),
127
+ .fifo_ren(m_fifo_ren),
128
+ .fifo_dout(m_fifo_dout),
129
+ .fifo_empty(),
130
+ .fifo_empty_n(m_fifo_empty_n),
131
+ .fifo_almost_empty(fifo_almost_empty),
132
+ .fifo_almost_empty_n(fifo_almost_empty_n),
133
+ .ram_wen(ram_wen),
134
+ .ram_w_addr(ram_w_addr),
135
+ .ram_din(ram_din),
136
+ .ram_ren(ram_ren),
137
+ .ram_r_addr(ram_r_addr),
138
+ .ram_dout(ram_dout),
139
+ .data_cnt(data_cnt)
140
+ );
141
+ end
142
+ else
143
+ begin
144
+ // 标准模式
145
+ fifo_based_on_ram_std #(
146
+ .fifo_depth(fifo_depth),
147
+ .fifo_data_width(fifo_data_width),
148
+ .almost_full_th(almost_full_th),
149
+ .almost_empty_th(almost_empty_th),
150
+ .simulation_delay(simulation_delay)
151
+ )fifo_based_on_ram_std_u(
152
+ .clk(clk),
153
+ .rst_n(rst_n),
154
+ .fifo_wen(fifo_wen),
155
+ .fifo_din(fifo_din),
156
+ .fifo_full(fifo_full),
157
+ .fifo_full_n(fifo_full_n),
158
+ .fifo_almost_full(fifo_almost_full),
159
+ .fifo_almost_full_n(fifo_almost_full_n),
160
+ .fifo_ren(fifo_ren),
161
+ .fifo_dout(fifo_dout),
162
+ .fifo_empty(fifo_empty),
163
+ .fifo_empty_n(fifo_empty_n),
164
+ .fifo_almost_empty(fifo_almost_empty),
165
+ .fifo_almost_empty_n(fifo_almost_empty_n),
166
+ .ram_wen(ram_wen),
167
+ .ram_w_addr(ram_w_addr),
168
+ .ram_din(ram_din),
169
+ .ram_ren(ram_ren),
170
+ .ram_r_addr(ram_r_addr),
171
+ .ram_dout(ram_dout),
172
+ .data_cnt(data_cnt)
173
+ );
174
+ end
175
+ endgenerate
176
+
177
+ /** 从fifo(仅FWFT模式下需要) **/
178
+ generate
179
+ if(fwft_mode == "true")
180
+ begin
181
+ fifo_show_ahead_buffer #(
182
+ .fifo_data_width(fifo_data_width),
183
+ .simulation_delay(simulation_delay)
184
+ )fifo_show_ahead_buffer_u(
185
+ .clk(clk),
186
+ .rst_n(rst_n),
187
+
188
+ .std_fifo_ren(m_fifo_ren),
189
+ .std_fifo_dout(m_fifo_dout),
190
+ .std_fifo_empty(~m_fifo_empty_n),
191
+
192
+ .fwft_fifo_ren(fifo_ren),
193
+ .fwft_fifo_dout(fifo_dout),
194
+ .fwft_fifo_empty(fifo_empty),
195
+ .fwft_fifo_empty_n(fifo_empty_n)
196
+ );
197
+ end
198
+ endgenerate
199
+
200
+ endmodule
CutestPanda_Opensoc/102_apb_i2c/rtl/fifo_based_on_lutram.v ADDED
@@ -0,0 +1,235 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ MIT License
3
+
4
+ Copyright (c) 2024 Panda, 2257691535@qq.com
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
23
+ */
24
+
25
+ `timescale 1ns / 1ps
26
+ /********************************************************************
27
+ 本模块: 基于lutram的同步fifo
28
+
29
+ 描述:
30
+ 全流水的高性能同步fifo
31
+ 基于lutram
32
+ 支持first word fall through特性(READ LA = 0)
33
+ 可选的固定阈值将满/将空信号
34
+
35
+ 注意:
36
+ 将满信号当存储计数 >= almost_full_th时有效
37
+ 将空信号当存储计数 <= almost_empty_th时有效
38
+ almost_full_th和almost_empty_th必须在[1, fifo_depth-1]范围内
39
+
40
+ 协议:
41
+ FIFO WRITE/READ
42
+
43
+ 作者: 陈家耀
44
+ 日期: 2023/10/13
45
+ ********************************************************************/
46
+
47
+
48
+ module fifo_based_on_lutram #(
49
+ parameter fwft_mode = "true", // 是否启用first word fall through特性
50
+ parameter integer fifo_depth = 32, // fifo深度(必须为2|4|8|16|...)
51
+ parameter integer fifo_data_width = 32, // fifo位宽
52
+ parameter integer almost_full_th = 20, // fifo将满阈值
53
+ parameter integer almost_empty_th = 5, // fifo将空阈值
54
+ parameter real simulation_delay = 1 // 仿真延时
55
+ )(
56
+ // 时钟和复位
57
+ input wire clk,
58
+ input wire rst_n,
59
+
60
+ // FIFO WRITE(fifo写端口)
61
+ input wire fifo_wen,
62
+ input wire[fifo_data_width-1:0] fifo_din,
63
+ output wire fifo_full,
64
+ output wire fifo_full_n,
65
+ output wire fifo_almost_full,
66
+ output wire fifo_almost_full_n,
67
+
68
+ // FIFO READ(fifo读端口)
69
+ input wire fifo_ren,
70
+ output wire[fifo_data_width-1:0] fifo_dout,
71
+ output wire fifo_empty,
72
+ output wire fifo_empty_n,
73
+ output wire fifo_almost_empty,
74
+ output wire fifo_almost_empty_n,
75
+
76
+ // 存储计数
77
+ output wire[clogb2(fifo_depth):0] data_cnt
78
+ );
79
+
80
+ // 计算log2(bit_depth)
81
+ function integer clogb2 (input integer bit_depth);
82
+ integer temp;
83
+ begin
84
+ temp = bit_depth;
85
+ for(clogb2 = -1;temp > 0;clogb2 = clogb2 + 1)
86
+ temp = temp >> 1;
87
+ end
88
+ endfunction
89
+
90
+ /** 参数 **/
91
+ localparam integer use_cnt_th = 8;
92
+
93
+ /** 空满标志和存储计数 **/
94
+ reg fifo_empty_reg;
95
+ reg fifo_full_reg;
96
+ reg fifo_almost_empty_reg;
97
+ reg fifo_almost_full_reg;
98
+ reg fifo_empty_n_reg;
99
+ reg fifo_full_n_reg;
100
+ reg fifo_almost_empty_n_reg;
101
+ reg fifo_almost_full_n_reg;
102
+ reg[clogb2(fifo_depth):0] data_cnt_regs;
103
+ reg[fifo_depth:0] data_cnt_onehot_regs;
104
+
105
+ assign {fifo_empty, fifo_full} = {fifo_empty_reg, fifo_full_reg};
106
+ assign {fifo_empty_n, fifo_full_n} = {fifo_empty_n_reg, fifo_full_n_reg};
107
+ assign {fifo_almost_empty, fifo_almost_full} = {fifo_almost_empty_reg, fifo_almost_full_reg};
108
+ assign {fifo_almost_empty_n, fifo_almost_full_n} = {fifo_almost_empty_n_reg, fifo_almost_full_n_reg};
109
+ assign data_cnt = data_cnt_regs;
110
+
111
+ always @(posedge clk or negedge rst_n)
112
+ begin
113
+ if(~rst_n)
114
+ begin
115
+ fifo_empty_reg <= 1'b1;
116
+ fifo_empty_n_reg <= 1'b0;
117
+ fifo_full_reg <= 1'b0;
118
+ fifo_full_n_reg <= 1'b1;
119
+ fifo_almost_empty_reg <= 1'b1;
120
+ fifo_almost_empty_n_reg <= 1'b0;
121
+ fifo_almost_full_reg <= 1'b0;
122
+ fifo_almost_full_n_reg <= 1'b1;
123
+
124
+ data_cnt_regs <= 0;
125
+ data_cnt_onehot_regs <= 1;
126
+ end
127
+ else if((fifo_wen & fifo_full_n_reg) ^ (fifo_ren & fifo_empty_n_reg))
128
+ begin
129
+ # simulation_delay;
130
+
131
+ if(fifo_wen & fifo_full_n_reg)
132
+ begin
133
+ // fifo数据增加1个
134
+ fifo_empty_reg <= 1'b0;
135
+ fifo_empty_n_reg <= 1'b1;
136
+ fifo_full_reg <= (fifo_depth >= use_cnt_th) ? data_cnt_regs == fifo_depth - 1:data_cnt_onehot_regs[fifo_depth-1];
137
+ fifo_full_n_reg <= (fifo_depth >= use_cnt_th) ? data_cnt_regs != fifo_depth - 1:(~data_cnt_onehot_regs[fifo_depth-1]);
138
+ fifo_almost_empty_reg <= (data_cnt_regs <= almost_empty_th - 1);
139
+ fifo_almost_empty_n_reg <= ~(data_cnt_regs <= almost_empty_th - 1);
140
+ fifo_almost_full_reg <= (data_cnt_regs >= almost_full_th - 1);
141
+ fifo_almost_full_n_reg <= ~(data_cnt_regs >= almost_full_th - 1);
142
+
143
+ data_cnt_regs <= data_cnt_regs + 1;
144
+ data_cnt_onehot_regs <= {data_cnt_onehot_regs[fifo_depth-1:0], 1'b0}; // 左移
145
+ end
146
+ else
147
+ begin
148
+ // fifo数据减少1个
149
+ fifo_empty_reg <= (fifo_depth >= use_cnt_th) ? data_cnt_regs == 1:data_cnt_onehot_regs[1];
150
+ fifo_empty_n_reg <= (fifo_depth >= use_cnt_th) ? data_cnt_regs != 1:(~data_cnt_onehot_regs[1]);
151
+ fifo_full_reg <= 1'b0;
152
+ fifo_full_n_reg <= 1'b1;
153
+ fifo_almost_empty_reg <= (data_cnt_regs <= almost_empty_th + 1);
154
+ fifo_almost_empty_n_reg <= ~(data_cnt_regs <= almost_empty_th + 1);
155
+ fifo_almost_full_reg <= (data_cnt_regs >= almost_full_th + 1);
156
+ fifo_almost_full_n_reg <= ~(data_cnt_regs >= almost_full_th + 1);
157
+
158
+ data_cnt_regs <= data_cnt_regs - 1;
159
+ data_cnt_onehot_regs <= {1'b0, data_cnt_onehot_regs[fifo_depth:1]}; // 右移
160
+ end
161
+ end
162
+ end
163
+
164
+ /** 读写指针 **/
165
+ reg[clogb2(fifo_depth-1):0] fifo_rptr;
166
+ reg[clogb2(fifo_depth-1):0] fifo_rptr_add1;
167
+ reg[clogb2(fifo_depth-1):0] fifo_wptr;
168
+ reg[fifo_depth-1:0] fifo_wptr_onehot;
169
+
170
+ always @(posedge clk or negedge rst_n)
171
+ begin
172
+ if(~rst_n)
173
+ fifo_rptr <= 0;
174
+ else if(fifo_ren & fifo_empty_n_reg)
175
+ #simulation_delay fifo_rptr <= fifo_rptr + 1;
176
+ end
177
+
178
+ always @(posedge clk or negedge rst_n)
179
+ begin
180
+ if(~rst_n)
181
+ fifo_rptr_add1 <= 1;
182
+ else if(fifo_ren & fifo_empty_n_reg)
183
+ #simulation_delay fifo_rptr_add1 <= fifo_rptr_add1 + 1;
184
+ end
185
+
186
+ always @(posedge clk or negedge rst_n)
187
+ begin
188
+ if(~rst_n)
189
+ fifo_wptr <= 0;
190
+ else if(fifo_wen & fifo_full_n_reg)
191
+ #simulation_delay fifo_wptr <= fifo_wptr + 1;
192
+ end
193
+
194
+ always @(posedge clk or negedge rst_n)
195
+ begin
196
+ if(~rst_n)
197
+ fifo_wptr_onehot <= 1;
198
+ else if(fifo_wen & fifo_full_n_reg)
199
+ #simulation_delay fifo_wptr_onehot <= {fifo_wptr_onehot[fifo_depth-2:0], fifo_wptr_onehot[fifo_depth-1]}; // 循环左移
200
+ end
201
+
202
+ /** 读写数据 **/
203
+ (* ram_style="distributed" *) reg[fifo_data_width-1:0] fifo_regs[fifo_depth-1:0];
204
+ reg[fifo_data_width-1:0] fifo_dout_regs;
205
+
206
+ assign fifo_dout = fifo_dout_regs;
207
+
208
+ always @(posedge clk)
209
+ begin
210
+ if(fifo_wen & fifo_full_n_reg)
211
+ #simulation_delay fifo_regs[fifo_wptr] <= fifo_din;
212
+ end
213
+
214
+ generate
215
+ if(fwft_mode == "true")
216
+ begin
217
+ always @(posedge clk)
218
+ begin
219
+ if({fifo_empty_n_reg, fifo_ren} != 2'b10)
220
+ #simulation_delay fifo_dout_regs <= fifo_empty_n_reg & (~(fifo_wen & ((fifo_depth >= use_cnt_th) ? data_cnt_regs == 1:data_cnt_onehot_regs[1]))) ?
221
+ fifo_regs[fifo_rptr_add1]:fifo_din;
222
+ end
223
+ end
224
+ else
225
+ begin
226
+ always @(posedge clk)
227
+ begin
228
+ if(fifo_ren & fifo_empty_n_reg)
229
+ #simulation_delay fifo_dout_regs <= fifo_regs[fifo_rptr];
230
+ end
231
+ end
232
+ endgenerate
233
+
234
+ endmodule
235
+
CutestPanda_Opensoc/102_apb_i2c/rtl/fifo_based_on_ram_std.v ADDED
@@ -0,0 +1,223 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ MIT License
3
+
4
+ Copyright (c) 2024 Panda, 2257691535@qq.com
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
23
+ */
24
+
25
+ `timescale 1ns / 1ps
26
+ /********************************************************************
27
+ 本模块: 基于ram的标准同步fifo控制器
28
+
29
+ 描述:
30
+ 全流水的高性能同步fifo控制器
31
+ 基于ram
32
+ 标准fifo(READ LA = 1|2)
33
+ 可选的固定阈值将满/将空信号
34
+
35
+ 注意:
36
+ 将满信号当存储计数 >= almost_full_th时有效
37
+ 将空信号当存储计数 <= almost_empty_th时有效
38
+ almost_full_th和almost_empty_th必须在[1, fifo_depth-1]范围内
39
+ 要求ram的读延迟=1clk
40
+
41
+ 协议:
42
+ FIFO WRITE/READ
43
+ MEM WRITE/READ
44
+
45
+ 作者: 陈家耀
46
+ 日期: 2023/10/29
47
+ ********************************************************************/
48
+
49
+
50
+ module fifo_based_on_ram_std #(
51
+ parameter integer fifo_depth = 32, // fifo深度(必须为2|4|8|16|...)
52
+ parameter integer fifo_data_width = 32, // fifo位宽
53
+ parameter integer almost_full_th = 20, // fifo将满阈值
54
+ parameter integer almost_empty_th = 5, // fifo将空阈值
55
+ parameter real simulation_delay = 1 // 仿真延时
56
+ )(
57
+ // 时钟和复位
58
+ input wire clk,
59
+ input wire rst_n,
60
+
61
+ // FIFO WRITE(fifo写端口)
62
+ input wire fifo_wen,
63
+ input wire[fifo_data_width-1:0] fifo_din,
64
+ output wire fifo_full,
65
+ output wire fifo_full_n,
66
+ output wire fifo_almost_full,
67
+ output wire fifo_almost_full_n,
68
+
69
+ // FIFO READ(fifo读端口)
70
+ input wire fifo_ren,
71
+ output wire[fifo_data_width-1:0] fifo_dout,
72
+ output wire fifo_empty,
73
+ output wire fifo_empty_n,
74
+ output wire fifo_almost_empty,
75
+ output wire fifo_almost_empty_n,
76
+
77
+ // MEM WRITE(ram写端口)
78
+ output wire ram_wen,
79
+ output wire[clogb2(fifo_depth-1):0] ram_w_addr,
80
+ output wire[fifo_data_width-1:0] ram_din,
81
+
82
+ // MEM RAD(ram读端口)
83
+ output wire ram_ren,
84
+ output wire[clogb2(fifo_depth-1):0] ram_r_addr,
85
+ input wire[fifo_data_width-1:0] ram_dout,
86
+
87
+ // 存储计数
88
+ output wire[clogb2(fifo_depth):0] data_cnt
89
+ );
90
+
91
+ // 计算log2(bit_depth)
92
+ function integer clogb2 (input integer bit_depth);
93
+ integer temp;
94
+ begin
95
+ temp = bit_depth;
96
+ for(clogb2 = -1;temp > 0;clogb2 = clogb2 + 1)
97
+ temp = temp >> 1;
98
+ end
99
+ endfunction
100
+
101
+ /** 参数 **/
102
+ localparam integer use_cnt_th = 8;
103
+
104
+ /** 空满标志和存储计数 **/
105
+ reg fifo_empty_reg;
106
+ reg fifo_full_reg;
107
+ reg fifo_almost_empty_reg;
108
+ reg fifo_almost_full_reg;
109
+ reg fifo_empty_n_reg;
110
+ reg fifo_full_n_reg;
111
+ reg fifo_almost_empty_n_reg;
112
+ reg fifo_almost_full_n_reg;
113
+ reg[clogb2(fifo_depth):0] data_cnt_regs;
114
+ reg[fifo_depth:0] data_cnt_onehot_regs;
115
+
116
+ assign {fifo_empty, fifo_full} = {fifo_empty_reg, fifo_full_reg};
117
+ assign {fifo_empty_n, fifo_full_n} = {fifo_empty_n_reg, fifo_full_n_reg};
118
+ assign {fifo_almost_empty, fifo_almost_full} = {fifo_almost_empty_reg, fifo_almost_full_reg};
119
+ assign {fifo_almost_empty_n, fifo_almost_full_n} = {fifo_almost_empty_n_reg, fifo_almost_full_n_reg};
120
+ assign data_cnt = data_cnt_regs;
121
+
122
+ always @(posedge clk or negedge rst_n)
123
+ begin
124
+ if(~rst_n)
125
+ begin
126
+ fifo_empty_reg <= 1'b1;
127
+ fifo_empty_n_reg <= 1'b0;
128
+ fifo_full_reg <= 1'b0;
129
+ fifo_full_n_reg <= 1'b1;
130
+ fifo_almost_empty_reg <= 1'b1;
131
+ fifo_almost_empty_n_reg <= 1'b0;
132
+ fifo_almost_full_reg <= 1'b0;
133
+ fifo_almost_full_n_reg <= 1'b1;
134
+
135
+ data_cnt_regs <= 0;
136
+ data_cnt_onehot_regs <= 1;
137
+ end
138
+ else if((fifo_wen & fifo_full_n_reg) ^ (fifo_ren & fifo_empty_n_reg))
139
+ begin
140
+ # simulation_delay;
141
+
142
+ if(fifo_wen & fifo_full_n_reg)
143
+ begin
144
+ // fifo数据增加1个
145
+ fifo_empty_reg <= 1'b0;
146
+ fifo_empty_n_reg <= 1'b1;
147
+ fifo_full_reg <= (fifo_depth >= use_cnt_th) ? data_cnt_regs == fifo_depth - 1:data_cnt_onehot_regs[fifo_depth-1];
148
+ fifo_full_n_reg <= (fifo_depth >= use_cnt_th) ? data_cnt_regs != fifo_depth - 1:(~data_cnt_onehot_regs[fifo_depth-1]);
149
+ fifo_almost_empty_reg <= (data_cnt_regs <= almost_empty_th - 1);
150
+ fifo_almost_empty_n_reg <= ~(data_cnt_regs <= almost_empty_th - 1);
151
+ fifo_almost_full_reg <= (data_cnt_regs >= almost_full_th - 1);
152
+ fifo_almost_full_n_reg <= ~(data_cnt_regs >= almost_full_th - 1);
153
+
154
+ data_cnt_regs <= data_cnt_regs + 1;
155
+ data_cnt_onehot_regs <= {data_cnt_onehot_regs[fifo_depth-1:0], 1'b0}; // 左移
156
+ end
157
+ else
158
+ begin
159
+ // fifo数据减少1个
160
+ fifo_empty_reg <= (fifo_depth >= use_cnt_th) ? data_cnt_regs == 1:data_cnt_onehot_regs[1];
161
+ fifo_empty_n_reg <= (fifo_depth >= use_cnt_th) ? data_cnt_regs != 1:(~data_cnt_onehot_regs[1]);
162
+ fifo_full_reg <= 1'b0;
163
+ fifo_full_n_reg <= 1'b1;
164
+ fifo_almost_empty_reg <= (data_cnt_regs <= almost_empty_th + 1);
165
+ fifo_almost_empty_n_reg <= ~(data_cnt_regs <= almost_empty_th + 1);
166
+ fifo_almost_full_reg <= (data_cnt_regs >= almost_full_th + 1);
167
+ fifo_almost_full_n_reg <= ~(data_cnt_regs >= almost_full_th + 1);
168
+
169
+ data_cnt_regs <= data_cnt_regs - 1;
170
+ data_cnt_onehot_regs <= {1'b0, data_cnt_onehot_regs[fifo_depth:1]}; // 右移
171
+ end
172
+ end
173
+ end
174
+
175
+ /** 读写指针 **/
176
+ reg[clogb2(fifo_depth-1):0] fifo_rptr;
177
+ reg[clogb2(fifo_depth-1):0] fifo_rptr_add1;
178
+ reg[clogb2(fifo_depth-1):0] fifo_wptr;
179
+ reg[fifo_depth-1:0] fifo_wptr_onehot;
180
+
181
+ always @(posedge clk or negedge rst_n)
182
+ begin
183
+ if(~rst_n)
184
+ fifo_rptr <= 0;
185
+ else if(fifo_ren & fifo_empty_n_reg)
186
+ #simulation_delay fifo_rptr <= fifo_rptr + 1;
187
+ end
188
+
189
+ always @(posedge clk or negedge rst_n)
190
+ begin
191
+ if(~rst_n)
192
+ fifo_rptr_add1 <= 1;
193
+ else if(fifo_ren & fifo_empty_n_reg)
194
+ #simulation_delay fifo_rptr_add1 <= fifo_rptr_add1 + 1;
195
+ end
196
+
197
+ always @(posedge clk or negedge rst_n)
198
+ begin
199
+ if(~rst_n)
200
+ fifo_wptr <= 0;
201
+ else if(fifo_wen & fifo_full_n_reg)
202
+ #simulation_delay fifo_wptr <= fifo_wptr + 1;
203
+ end
204
+
205
+ always @(posedge clk or negedge rst_n)
206
+ begin
207
+ if(~rst_n)
208
+ fifo_wptr_onehot <= 1;
209
+ else if(fifo_wen & fifo_full_n_reg)
210
+ #simulation_delay fifo_wptr_onehot <= {fifo_wptr_onehot[fifo_depth-2:0], fifo_wptr_onehot[fifo_depth-1]}; // 循环左移
211
+ end
212
+
213
+ /** 读写数据 **/
214
+ assign ram_wen = fifo_wen & fifo_full_n_reg;
215
+ assign ram_w_addr = fifo_wptr;
216
+ assign ram_din = fifo_din;
217
+
218
+ assign ram_ren = fifo_ren & fifo_empty_n_reg;
219
+ assign ram_r_addr = fifo_rptr;
220
+ assign fifo_dout = ram_dout;
221
+
222
+ endmodule
223
+
CutestPanda_Opensoc/102_apb_i2c/rtl/fifo_show_ahead_buffer.v ADDED
@@ -0,0 +1,122 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ MIT License
3
+
4
+ Copyright (c) 2024 Panda, 2257691535@qq.com
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
23
+ */
24
+
25
+ `timescale 1ns / 1ps
26
+ /********************************************************************
27
+ 本模块: fifo FWFT缓冲区
28
+
29
+ 描述:
30
+ 用于将读延迟为1clk的fifo转换为FWFT的fifo
31
+
32
+ 注意:
33
+
34
+
35
+ 协议:
36
+ FIFO READ
37
+
38
+ 作者: 陈家耀
39
+ 日期: 2024/05/30
40
+ ********************************************************************/
41
+
42
+
43
+ module fifo_show_ahead_buffer #(
44
+ parameter integer fifo_data_width = 32, // fifo位宽
45
+ parameter real simulation_delay = 1 // 仿真延时
46
+ )(
47
+ // 时钟和复位
48
+ input wire clk,
49
+ input wire rst_n,
50
+
51
+ // 标准fifo的读端口
52
+ output wire std_fifo_ren,
53
+ input wire[fifo_data_width-1:0] std_fifo_dout,
54
+ input wire std_fifo_empty,
55
+
56
+ // FWFT fifo的读端口
57
+ input wire fwft_fifo_ren,
58
+ output wire[fifo_data_width-1:0] fwft_fifo_dout,
59
+ output wire fwft_fifo_empty,
60
+ output wire fwft_fifo_empty_n
61
+ );
62
+
63
+ /** 寄存器buffer **/
64
+ reg std_fifo_rvld; // 标准fifo有效读(指示)
65
+ reg[fifo_data_width-1:0] regs_buffer[1:0]; // 寄存器buffer
66
+ reg[2:0] buffer_data_cnt; // buffer存储计数(3'b001 -> 0, 3'b010 -> 1, 3'b100 -> 2)
67
+ wire[2:0] buffer_data_cnt_sub1; // buffer存储计数 - 1(3'b001 -> 0, 3'b010 -> 1, 3'b100 -> 2)
68
+
69
+ assign buffer_data_cnt_sub1 = {buffer_data_cnt[0], buffer_data_cnt[2:1]};
70
+
71
+ // 标准fifo有效读(指示)
72
+ always @(posedge clk or negedge rst_n)
73
+ begin
74
+ if(~rst_n)
75
+ std_fifo_rvld <= 1'b0;
76
+ else
77
+ # simulation_delay std_fifo_rvld <= std_fifo_ren & (~std_fifo_empty);
78
+ end
79
+
80
+ // 寄存器buffer
81
+ always @(posedge clk)
82
+ begin
83
+ if(std_fifo_rvld & ((fwft_fifo_ren & fwft_fifo_empty_n) ? buffer_data_cnt_sub1[0]:buffer_data_cnt[0]))
84
+ # simulation_delay regs_buffer[0] <= std_fifo_dout;
85
+ else if(fwft_fifo_ren & fwft_fifo_empty_n)
86
+ # simulation_delay regs_buffer[0] <= regs_buffer[1];
87
+ end
88
+
89
+ always @(posedge clk)
90
+ begin
91
+ if(std_fifo_rvld & ((fwft_fifo_ren & fwft_fifo_empty_n) ? buffer_data_cnt_sub1[1]:buffer_data_cnt[1]))
92
+ # simulation_delay regs_buffer[1] <= std_fifo_dout;
93
+ end
94
+
95
+ // buffer存储计数独热码
96
+ always @(posedge clk or negedge rst_n)
97
+ begin
98
+ if(~rst_n)
99
+ buffer_data_cnt <= 3'b001;
100
+ else if(std_fifo_rvld ^ (fwft_fifo_ren & fwft_fifo_empty_n))
101
+ # simulation_delay buffer_data_cnt <= std_fifo_rvld ? {buffer_data_cnt[1:0], buffer_data_cnt[2]}:{buffer_data_cnt[0], buffer_data_cnt[2:1]};
102
+ end
103
+
104
+ /** 标准fifo的读端口 **/
105
+ assign std_fifo_ren = buffer_data_cnt[0] | fwft_fifo_ren; // buffer存储计数==0或者FWFT-fifo读使能时标准fifo读使能有效
106
+
107
+ /** FWFT fifo的读端口 **/
108
+ reg fwft_fifo_empty_n_reg;
109
+
110
+ assign fwft_fifo_dout = regs_buffer[0];
111
+ assign fwft_fifo_empty = buffer_data_cnt[0];
112
+ assign fwft_fifo_empty_n = fwft_fifo_empty_n_reg;
113
+
114
+ always @(posedge clk or negedge rst_n)
115
+ begin
116
+ if(~rst_n)
117
+ fwft_fifo_empty_n_reg <= 1'b0;
118
+ else if(std_fifo_rvld ^ (fwft_fifo_ren & fwft_fifo_empty_n))
119
+ # simulation_delay fwft_fifo_empty_n_reg <= std_fifo_rvld ? 1'b1:(~buffer_data_cnt[1]);
120
+ end
121
+
122
+ endmodule
CutestPanda_Opensoc/102_apb_i2c/rtl/i2c_ctrler.v ADDED
@@ -0,0 +1,363 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ MIT License
3
+
4
+ Copyright (c) 2024 Panda, 2257691535@qq.com
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
23
+ */
24
+
25
+ `timescale 1ns / 1ps
26
+ /********************************************************************
27
+ 本模块: I2C控制器
28
+
29
+ 描述:
30
+ 通用IC2控制器
31
+ 支持7位/10位地址
32
+
33
+ 发送fifo数据包格式:
34
+ 写数据 -> last0 xxxx_xxx0 地址阶段
35
+ [last0 xxxx_xxxx 地址阶段(仅10位地址时需要)]
36
+ last0 xxxx_xxxx 数据阶段
37
+ ...
38
+ last1 xxxx_xxxx 数据阶段
39
+ 读数据 -> last0 xxxx_xxx1 地址阶段
40
+ [last0 xxxx_xxxx 地址阶段(仅10位地址时需要)]
41
+ last1 8位待读取字节数
42
+
43
+ 注意:
44
+ 每个I2C数据包不能超过15字节
45
+
46
+ 协议:
47
+ I2C MASTER
48
+
49
+ 作者: 陈家耀
50
+ 日期: 2024/06/14
51
+ ********************************************************************/
52
+
53
+
54
+ module i2c_ctrler #(
55
+ parameter integer addr_bits_n = 7, // 地址位数(7|10)
56
+ parameter real simulation_delay = 1 // 仿真延时
57
+ )(
58
+ // 时钟和复位
59
+ input wire clk,
60
+ input wire resetn,
61
+
62
+ // I2C时钟分频系数
63
+ // 分频数 = (分频系数 + 1) * 2
64
+ // 断言:分频系数>=1!
65
+ input wire[7:0] i2c_scl_div_rate,
66
+
67
+ // 发送fifo读端口
68
+ output wire tx_fifo_ren,
69
+ input wire tx_fifo_empty,
70
+ input wire[7:0] tx_fifo_dout,
71
+ input wire tx_fifo_dout_last,
72
+ // 接收fifo写端口
73
+ output wire rx_fifo_wen,
74
+ input wire rx_fifo_full,
75
+ output wire[7:0] rx_fifo_din,
76
+
77
+ // I2C发送完成指示
78
+ output wire i2c_tx_done,
79
+ output wire[3:0] i2c_tx_bytes_n,
80
+ // I2C接收完成指示
81
+ output wire i2c_rx_done,
82
+ output wire[3:0] i2c_rx_bytes_n,
83
+ // I2C从机响应错误
84
+ output wire i2c_slave_resp_err,
85
+ // I2C接收溢出
86
+ output wire i2c_rx_overflow,
87
+
88
+ // I2C主机接口
89
+ // scl
90
+ output wire scl_t, // 1'b1为输入, 1'b0为输出
91
+ input wire scl_i,
92
+ output wire scl_o,
93
+ // sda
94
+ output wire sda_t, // 1'b1为输入, 1'b0为输出
95
+ input wire sda_i,
96
+ output wire sda_o
97
+ );
98
+
99
+ /** 常量 **/
100
+ // 状态常量
101
+ localparam I2C_CTRLER_STS_IDLE = 2'b00; // 状态:空闲
102
+ localparam I2C_CTRLER_STS_LOAD = 2'b01; // 状态:加载数据
103
+ localparam I2C_CTRLER_STS_RX_TX = 2'b10; // 状态:收发
104
+ localparam I2C_CTRLER_STS_DONE = 2'b11; // 状态:完成
105
+
106
+ /** 流程控制 **/
107
+ reg[1:0] i2c_ctrler_sts; // 控制器状态
108
+ reg[2:0] tx_fifo_ren_onehot; // 发送fifo读使能(独热码)
109
+ reg[2:0] i2c_if_stage_onehot; // i2c主接口控制器阶段(独热码)
110
+ wire i2c_if_ctrler_done; // i2c主接口控制器完成(指示)
111
+ reg is_rd_trans; // 是否读传输
112
+ reg last_loaded; // 载入的last信号
113
+ wire is_addr_stage; // 是否处于地址阶段
114
+ reg[3:0] bytes_n_to_rx; // 待接收字节数
115
+ reg trans_first; // 收发第1个字节(标志)
116
+ reg[3:0] trans_bytes_n; // 已收发的字节数
117
+ reg[3:0] trans_bytes_n_add1; // 已收发的字节数 + 1
118
+
119
+ assign tx_fifo_ren = tx_fifo_ren_onehot[1];
120
+
121
+ // 控制器状态
122
+ always @(posedge clk or negedge resetn)
123
+ begin
124
+ if(~resetn)
125
+ i2c_ctrler_sts <= I2C_CTRLER_STS_IDLE;
126
+ else
127
+ begin
128
+ # simulation_delay;
129
+
130
+ case(i2c_ctrler_sts)
131
+ I2C_CTRLER_STS_IDLE: // 状态:空闲
132
+ if(~tx_fifo_empty)
133
+ i2c_ctrler_sts <= I2C_CTRLER_STS_LOAD; // -> 状态:加载数据
134
+ I2C_CTRLER_STS_LOAD: // 状态:加载数据
135
+ if(tx_fifo_ren_onehot[2])
136
+ i2c_ctrler_sts <= I2C_CTRLER_STS_RX_TX; // -> 状态:收发
137
+ I2C_CTRLER_STS_RX_TX: // 状态:收发
138
+ if(i2c_if_stage_onehot[2] & i2c_if_ctrler_done)
139
+ i2c_ctrler_sts <= I2C_CTRLER_STS_DONE; // -> 状态:完成
140
+ I2C_CTRLER_STS_DONE:
141
+ begin
142
+ if(is_rd_trans)
143
+ begin
144
+ if(last_loaded)
145
+ i2c_ctrler_sts <= (trans_bytes_n == bytes_n_to_rx) ? I2C_CTRLER_STS_IDLE: // -> 状态:空闲
146
+ I2C_CTRLER_STS_RX_TX; // -> 状态:收发
147
+ else
148
+ i2c_ctrler_sts <= I2C_CTRLER_STS_LOAD; // -> 状态:加载数据
149
+ end
150
+ else
151
+ i2c_ctrler_sts <= last_loaded ? I2C_CTRLER_STS_IDLE: // -> 状态:空闲
152
+ I2C_CTRLER_STS_LOAD; // -> 状态:加载数据
153
+ end
154
+ default:
155
+ i2c_ctrler_sts <= I2C_CTRLER_STS_IDLE;
156
+ endcase
157
+ end
158
+ end
159
+
160
+ // 发送fifo读使能(独热码)
161
+ always @(posedge clk or negedge resetn)
162
+ begin
163
+ if(~resetn)
164
+ tx_fifo_ren_onehot <= 3'b001;
165
+ else if((tx_fifo_ren_onehot[0] & (i2c_ctrler_sts == I2C_CTRLER_STS_LOAD)) | (tx_fifo_ren_onehot[1] & (~tx_fifo_empty)) | tx_fifo_ren_onehot[2])
166
+ # simulation_delay tx_fifo_ren_onehot <= {tx_fifo_ren_onehot[1:0], tx_fifo_ren_onehot[2]};
167
+ end
168
+
169
+ // i2c主接口控制器阶段(独热码)
170
+ always @(posedge clk or negedge resetn)
171
+ begin
172
+ if(~resetn)
173
+ i2c_if_stage_onehot <= 3'b001;
174
+ else if((i2c_if_stage_onehot[0] & (i2c_ctrler_sts == I2C_CTRLER_STS_RX_TX)) | i2c_if_stage_onehot[1] | (i2c_if_stage_onehot[2] & i2c_if_ctrler_done))
175
+ # simulation_delay i2c_if_stage_onehot <= {i2c_if_stage_onehot[1:0], i2c_if_stage_onehot[2]};
176
+ end
177
+
178
+ // 收发第1个字节(标志)
179
+ always @(posedge clk)
180
+ begin
181
+ if(i2c_ctrler_sts == I2C_CTRLER_STS_IDLE)
182
+ # simulation_delay trans_first <= 1'b1;
183
+ else if(trans_first)
184
+ # simulation_delay trans_first <= ~(i2c_if_stage_onehot[2] & i2c_if_ctrler_done);
185
+ end
186
+
187
+ // 已收发的字节数
188
+ always @(posedge clk)
189
+ begin
190
+ if(i2c_ctrler_sts == I2C_CTRLER_STS_IDLE)
191
+ # simulation_delay trans_bytes_n <= 4'd0;
192
+ else if(i2c_if_stage_onehot[2] & i2c_if_ctrler_done & (~is_addr_stage))
193
+ # simulation_delay trans_bytes_n <= trans_bytes_n + 4'd1;
194
+ end
195
+ // 已收发的字节数 + 1
196
+ always @(posedge clk)
197
+ begin
198
+ if(i2c_ctrler_sts == I2C_CTRLER_STS_IDLE)
199
+ # simulation_delay trans_bytes_n_add1 <= 4'd1;
200
+ else if(i2c_if_stage_onehot[2] & i2c_if_ctrler_done & (~is_addr_stage))
201
+ # simulation_delay trans_bytes_n_add1 <= trans_bytes_n_add1 + 4'd1;
202
+ end
203
+
204
+ /** 数据载入 **/
205
+ reg first_load; // 本I2C数据包第1次载入数据(标志)
206
+ reg[7:0] data_loaded; // 载入的数据
207
+ wire resp_dire; // 响应方向(1'b0 -> 主机接收响应, 1'b1 -> 主机发送响应)
208
+
209
+ assign resp_dire = is_addr_stage ? 1'b0:is_rd_trans;
210
+
211
+ // 本I2C数据包第1次载入数据(标志)
212
+ always @(posedge clk)
213
+ begin
214
+ if(i2c_ctrler_sts == I2C_CTRLER_STS_IDLE)
215
+ # simulation_delay first_load <= 1'b1;
216
+ else if(first_load)
217
+ # simulation_delay first_load <= ~tx_fifo_ren_onehot[2];
218
+ end
219
+
220
+ // 载入的数据
221
+ // 载入的last信号
222
+ always @(posedge clk)
223
+ begin
224
+ if(tx_fifo_ren_onehot[2])
225
+ # simulation_delay {last_loaded, data_loaded} <= {tx_fifo_dout_last, tx_fifo_dout};
226
+ end
227
+
228
+ // 是否读传输
229
+ always @(posedge clk)
230
+ begin
231
+ if(tx_fifo_ren_onehot[2] & first_load)
232
+ # simulation_delay is_rd_trans <= tx_fifo_dout[0];
233
+ end
234
+
235
+ // 是否处于地址阶段
236
+ generate
237
+ if(addr_bits_n == 7) // 7位地址
238
+ begin
239
+ reg[1:0] is_addr_stage_onehot;
240
+
241
+ assign is_addr_stage = is_addr_stage_onehot[0];
242
+
243
+ always @(posedge clk)
244
+ begin
245
+ if(i2c_ctrler_sts == I2C_CTRLER_STS_IDLE)
246
+ # simulation_delay is_addr_stage_onehot <= 2'b01;
247
+ else if((~is_addr_stage_onehot[1]) & (i2c_if_stage_onehot[2] & i2c_if_ctrler_done))
248
+ # simulation_delay is_addr_stage_onehot <= {is_addr_stage_onehot[0], is_addr_stage_onehot[1]};
249
+ end
250
+ end
251
+ else // 10位地址
252
+ begin
253
+ reg[2:0] is_addr_stage_onehot;
254
+
255
+ assign is_addr_stage = ~is_addr_stage_onehot[2];
256
+
257
+ always @(posedge clk)
258
+ begin
259
+ if(i2c_ctrler_sts == I2C_CTRLER_STS_IDLE)
260
+ # simulation_delay is_addr_stage_onehot <= 3'b001;
261
+ else if((~is_addr_stage_onehot[2]) & (i2c_if_stage_onehot[2] & i2c_if_ctrler_done))
262
+ # simulation_delay is_addr_stage_onehot <= {is_addr_stage_onehot[1:0], is_addr_stage_onehot[2]};
263
+ end
264
+ end
265
+ endgenerate
266
+
267
+ // 待接收字节数
268
+ always @(posedge clk)
269
+ begin
270
+ if(tx_fifo_ren_onehot[2] & tx_fifo_dout_last & is_rd_trans)
271
+ # simulation_delay bytes_n_to_rx <= tx_fifo_dout[3:0];
272
+ end
273
+
274
+ /** I2C主接口 **/
275
+ wire[7:0] byte_recv; // 接收到数据
276
+ reg i2c_rx_overflow_reg; // I2C接收溢出
277
+ wire i2c_if_ctrler_start; // i2c主接口控制器开始(指示)
278
+ reg[1:0] i2c_if_ctrler_mode; // i2c主接口控制器传输模式(2'b00 -> 带起始位, 2'b01 -> 带结束位, 2'b10 -> 正常, 2'b11 -> 预留)
279
+ wire i2c_if_ctrler_dire; // i2c主接口控制器传输方向(1'b0 -> 发送, 1'b1 -> 接收)
280
+
281
+ assign rx_fifo_wen = i2c_if_ctrler_done & resp_dire;
282
+ assign rx_fifo_din = byte_recv;
283
+
284
+ assign i2c_rx_overflow = i2c_rx_overflow_reg;
285
+
286
+ assign i2c_if_ctrler_start = i2c_if_stage_onehot[1];
287
+ assign i2c_if_ctrler_dire = resp_dire;
288
+
289
+ // I2C接收溢出
290
+ always @(posedge clk or negedge resetn)
291
+ begin
292
+ if(~resetn)
293
+ i2c_rx_overflow_reg <= 1'b0;
294
+ else
295
+ # simulation_delay i2c_rx_overflow_reg <= rx_fifo_wen & rx_fifo_full;
296
+ end
297
+
298
+ // i2c主接口控制器传输模式
299
+ always @(posedge clk)
300
+ begin
301
+ if(trans_first)
302
+ # simulation_delay i2c_if_ctrler_mode <= 2'b00;
303
+ else if(is_rd_trans ? (last_loaded & (trans_bytes_n_add1 == bytes_n_to_rx)):last_loaded)
304
+ # simulation_delay i2c_if_ctrler_mode <= 2'b01;
305
+ else
306
+ # simulation_delay i2c_if_ctrler_mode <= 2'b10;
307
+ end
308
+
309
+ // i2c主接口控制器
310
+ i2c_master_if #(
311
+ .simulation_delay(simulation_delay)
312
+ )i2c_master_if_u(
313
+ .clk(clk),
314
+ .resetn(resetn),
315
+
316
+ .i2c_scl_div_rate(i2c_scl_div_rate),
317
+
318
+ .ctrler_start(i2c_if_ctrler_start),
319
+ .ctrler_idle(),
320
+ .ctrler_done(i2c_if_ctrler_done),
321
+
322
+ .mode(i2c_if_ctrler_mode),
323
+ .direction(i2c_if_ctrler_dire),
324
+ .byte_to_send(data_loaded),
325
+ .byte_recv(byte_recv),
326
+
327
+ .i2c_slave_resp_err(i2c_slave_resp_err),
328
+
329
+ .scl_t(scl_t),
330
+ .scl_i(scl_i),
331
+ .scl_o(scl_o),
332
+ .sda_t(sda_t),
333
+ .sda_i(sda_i),
334
+ .sda_o(sda_o)
335
+ );
336
+
337
+ /** I2C收发完成指示 **/
338
+ reg i2c_tx_done_reg; // I2C发送完成指示
339
+ reg i2c_rx_done_reg; // I2C接收完成指示
340
+
341
+ assign i2c_tx_done = i2c_tx_done_reg;
342
+ assign i2c_tx_bytes_n = trans_bytes_n;
343
+ assign i2c_rx_done = i2c_rx_done_reg;
344
+ assign i2c_rx_bytes_n = trans_bytes_n;
345
+
346
+ // I2C发送完成指示
347
+ always @(posedge clk or negedge resetn)
348
+ begin
349
+ if(~resetn)
350
+ i2c_tx_done_reg <= 1'b0;
351
+ else
352
+ # simulation_delay i2c_tx_done_reg <= (i2c_ctrler_sts == I2C_CTRLER_STS_DONE) & (~is_rd_trans) & last_loaded;
353
+ end
354
+ // I2C接收完成指示
355
+ always @(posedge clk or negedge resetn)
356
+ begin
357
+ if(~resetn)
358
+ i2c_rx_done_reg <= 1'b0;
359
+ else
360
+ # simulation_delay i2c_rx_done_reg <= (i2c_ctrler_sts == I2C_CTRLER_STS_DONE) & is_rd_trans & last_loaded & (trans_bytes_n == bytes_n_to_rx);
361
+ end
362
+
363
+ endmodule
CutestPanda_Opensoc/102_apb_i2c/rtl/i2c_master_if.v ADDED
@@ -0,0 +1,358 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ MIT License
3
+
4
+ Copyright (c) 2024 Panda, 2257691535@qq.com
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
23
+ */
24
+
25
+ `timescale 1ns / 1ps
26
+ /********************************************************************
27
+ 本模块: I2C主接口
28
+
29
+ 描述:
30
+ 根据传输方向和待发送数据来驱动I2C主接口
31
+
32
+ 注意:
33
+
34
+
35
+ 协议:
36
+ I2C MASTER
37
+
38
+ 作者: 陈家耀
39
+ 日期: 2024/06/15
40
+ ********************************************************************/
41
+
42
+
43
+ module i2c_master_if #(
44
+ parameter real simulation_delay = 1 // 仿真延时
45
+ )(
46
+ // 时钟和复位
47
+ input wire clk,
48
+ input wire resetn,
49
+
50
+ // I2C时钟分频系数
51
+ // 分频数 = (分频系数 + 1) * 2
52
+ // 断言:分频系数>=1!
53
+ input wire[7:0] i2c_scl_div_rate,
54
+
55
+ // 流程控制
56
+ input wire ctrler_start,
57
+ output wire ctrler_idle,
58
+ output wire ctrler_done,
59
+
60
+ // 收发控制
61
+ input wire[1:0] mode, // 传输模式(2'b00 -> 带起始位, 2'b01 -> 带结束位, 2'b10 -> 正常, 2'b11 -> 预留)
62
+ input wire direction, // 传输方向(1'b0 -> 发送, 1'b1 -> 接收)
63
+ input wire[7:0] byte_to_send, // 待发送数据
64
+ output wire[7:0] byte_recv, // 接收到的数据
65
+
66
+ // I2C从机响应错误
67
+ output wire i2c_slave_resp_err,
68
+
69
+ // I2C主机接口
70
+ // scl
71
+ output wire scl_t, // 1'b1为输入, 1'b0为输出
72
+ input wire scl_i,
73
+ output wire scl_o,
74
+ // sda
75
+ output wire sda_t, // 1'b1为输入, 1'b0为输出
76
+ input wire sda_i,
77
+ output wire sda_o
78
+ );
79
+
80
+ /** 常量 **/
81
+ // 模式常量
82
+ localparam MODE_WITH_START = 2'b00; // 模式:带起始位
83
+ localparam MODE_WITH_STOP = 2'b01; // 模式:带结束位
84
+ localparam MODE_NORMAL = 2'b10; // 模式:正常
85
+ // 控制器状态常量
86
+ localparam CTRLER_STS_IDLE = 3'b000; // 状态:空闲
87
+ localparam CTRLER_STS_START = 3'b001; // 状态:起始位
88
+ localparam CTRLER_STS_DATA = 3'b010; // 状态:数据位
89
+ localparam CTRLER_STS_RESP = 3'b011; // 状态:响应
90
+ localparam CTRLER_STS_STOP = 3'b100; // 状态:停止位
91
+ localparam CTRLER_STS_DONE = 3'b101; // 状态:完成
92
+
93
+ /** 载入的控制信息 **/
94
+ reg[1:0] mode_latched; // 锁存的传输模式(2'b00 -> 带起始位, 2'b01 -> 带结束位, 2'b10 -> 正常, 2'b11 -> 预留)
95
+ reg direction_latched; // 锁存的传输方向(1'b0 -> 发送, 1'b1 -> 接收)
96
+
97
+ // 锁存的传输模式
98
+ always @(posedge clk)
99
+ begin
100
+ if(ctrler_idle & ctrler_start)
101
+ # simulation_delay mode_latched <= mode;
102
+ end
103
+ // 锁存的传输方向
104
+ always @(posedge clk)
105
+ begin
106
+ if(ctrler_idle & ctrler_start)
107
+ # simulation_delay direction_latched <= direction;
108
+ end
109
+
110
+ /** 流程控制 **/
111
+ reg[2:0] i2c_if_ctrler_sts; // 控制器状态
112
+ wire rx_tx_byte_done; // 字节收发完成(指示)
113
+ wire resp_disposed; // 响应处理完成(指示)
114
+ wire stop_disposed; // 停止位处理完成(指示)
115
+ reg ctrler_idle_reg; // 控制器空闲(标志)
116
+ reg ctrler_done_reg; // 控制器完成(指示)
117
+
118
+ assign ctrler_idle = ctrler_idle_reg;
119
+ assign ctrler_done = ctrler_done_reg;
120
+
121
+ // 控制器状态
122
+ always @(posedge clk or negedge resetn)
123
+ begin
124
+ if(~resetn)
125
+ i2c_if_ctrler_sts <= CTRLER_STS_IDLE;
126
+ else
127
+ begin
128
+ # simulation_delay;
129
+
130
+ case(i2c_if_ctrler_sts)
131
+ CTRLER_STS_IDLE: // 状态:空闲
132
+ if(ctrler_start)
133
+ i2c_if_ctrler_sts <= CTRLER_STS_START; // -> 状态:起始位
134
+ CTRLER_STS_START: // 状态:起始位
135
+ i2c_if_ctrler_sts <= CTRLER_STS_DATA; // -> 状态:数据位
136
+ CTRLER_STS_DATA: // 状态:数据位
137
+ if(rx_tx_byte_done)
138
+ i2c_if_ctrler_sts <= CTRLER_STS_RESP; // -> 状态:响应
139
+ CTRLER_STS_RESP: // 状态:响应
140
+ if(resp_disposed)
141
+ i2c_if_ctrler_sts <= CTRLER_STS_STOP; // -> 状态:停止位
142
+ CTRLER_STS_STOP: // 状态:停止位
143
+ if((mode_latched == MODE_WITH_STOP) ? stop_disposed:1'b1)
144
+ i2c_if_ctrler_sts <= CTRLER_STS_DONE; // -> 状态:完成
145
+ CTRLER_STS_DONE: // 状态:完成
146
+ i2c_if_ctrler_sts <= CTRLER_STS_IDLE; // -> 状态:空闲
147
+ default:
148
+ i2c_if_ctrler_sts <= CTRLER_STS_IDLE;
149
+ endcase
150
+ end
151
+ end
152
+
153
+ // 控制器空闲(标志)
154
+ always @(posedge clk or negedge resetn)
155
+ begin
156
+ if(~resetn)
157
+ ctrler_idle_reg <= 1'b1;
158
+ else
159
+ # simulation_delay ctrler_idle_reg <= ctrler_idle_reg ? (~ctrler_start):ctrler_done_reg;
160
+ end
161
+ // 控制器完成(指示)
162
+ always @(posedge clk or negedge resetn)
163
+ begin
164
+ if(~resetn)
165
+ ctrler_done_reg <= 1'b0;
166
+ else
167
+ # simulation_delay ctrler_done_reg <= (i2c_if_ctrler_sts == CTRLER_STS_STOP) & ((mode_latched == MODE_WITH_STOP) ? stop_disposed:1'b1);
168
+ end
169
+
170
+ /** 数据收发控制 **/
171
+ reg shift_send_bit; // 待发送数据移位标志
172
+ reg sample_recv_bit; // 待接收数据采样标志
173
+ reg[7:0] send_byte_buffer; // 待发送数据缓冲区
174
+ reg[7:0] recv_byte_buffer; // 待接收数据缓冲区
175
+ reg[7:0] rx_tx_stage_onehot; // 收发进程独热码
176
+
177
+ assign byte_recv = recv_byte_buffer;
178
+
179
+ assign rx_tx_byte_done = rx_tx_stage_onehot[7] & sample_recv_bit;
180
+
181
+ // 待发送数据缓冲区
182
+ always @(posedge clk)
183
+ begin
184
+ if(ctrler_idle & ctrler_start)
185
+ # simulation_delay send_byte_buffer <= byte_to_send;
186
+ else if(shift_send_bit)
187
+ # simulation_delay send_byte_buffer <= {send_byte_buffer[6:0], 1'bx};
188
+ end
189
+ // 待接收数据缓冲区
190
+ always @(posedge clk)
191
+ begin
192
+ if(sample_recv_bit)
193
+ # simulation_delay recv_byte_buffer <= {recv_byte_buffer[6:0], sda_i};
194
+ end
195
+
196
+ // 收发进程独热码
197
+ always @(posedge clk or negedge resetn)
198
+ begin
199
+ if(~resetn)
200
+ rx_tx_stage_onehot <= 8'b0000_0001;
201
+ else if(sample_recv_bit)
202
+ # simulation_delay rx_tx_stage_onehot <= {rx_tx_stage_onehot[6:0], rx_tx_stage_onehot[7]};
203
+ end
204
+
205
+ /** 响应控制 **/
206
+ reg[7:0] resp_div_cnt; // 响应时scl分频计数器
207
+ reg[5:0] resp_stage_onehot; // 响应进程独热码(6'b000001 -> SCL高, 6'b000010 -> SCL高, 6'b000100 -> SCL低, 6'b001000 -> SCL低且输出响应, 6'b010000 -> SCL高, 6'b100000 -> SCL高)
208
+ reg i2c_slave_resp_err_reg; // I2C从机响应错误
209
+
210
+ assign i2c_slave_resp_err = i2c_slave_resp_err_reg;
211
+
212
+ assign resp_disposed = resp_stage_onehot[5];
213
+
214
+ // 响应时scl分频计数器
215
+ always @(posedge clk)
216
+ begin
217
+ if((i2c_if_ctrler_sts == CTRLER_STS_RESP) & (resp_stage_onehot[0] | resp_stage_onehot[2]))
218
+ # simulation_delay resp_div_cnt <= resp_div_cnt + 8'd1;
219
+ else
220
+ # simulation_delay resp_div_cnt <= 8'd0;
221
+ end
222
+
223
+ // 响应进程独热码
224
+ always @(posedge clk or negedge resetn)
225
+ begin
226
+ if(~resetn)
227
+ resp_stage_onehot <= 6'b00_00_01;
228
+ else if((i2c_if_ctrler_sts == CTRLER_STS_RESP) &
229
+ ((resp_stage_onehot[0] | resp_stage_onehot[2]) ? (resp_div_cnt == i2c_scl_div_rate):1'b1))
230
+ # simulation_delay resp_stage_onehot <= {resp_stage_onehot[4:0], resp_stage_onehot[5]};
231
+ end
232
+
233
+ // I2C从机响应错误
234
+ always @(posedge clk or negedge resetn)
235
+ begin
236
+ if(~resetn)
237
+ i2c_slave_resp_err_reg <= 1'b0;
238
+ else
239
+ # simulation_delay i2c_slave_resp_err_reg <= (i2c_if_ctrler_sts == CTRLER_STS_RESP) & resp_stage_onehot[4] & sda_i;
240
+ end
241
+
242
+ /** 停止位控制 **/
243
+ reg[7:0] stop_div_cnt; // 停止位scl分频计数器
244
+ reg[5:0] stop_stage_onehot; // 停止位进程独热码(6'b000001 -> SCL高, 6'b000010 -> SCL高, 6'b000100 -> SCL低, 6'b001000 -> SCL低且SDA低, 6'b010000 -> SCL高, 6'b100000 -> SCL高且SDA高)
245
+
246
+ assign stop_disposed = stop_stage_onehot[5];
247
+
248
+ // 停止位scl分频计数器
249
+ always @(posedge clk)
250
+ begin
251
+ if((i2c_if_ctrler_sts == CTRLER_STS_STOP) & (mode_latched == MODE_WITH_STOP) &
252
+ (stop_stage_onehot[0] | stop_stage_onehot[2]))
253
+ # simulation_delay stop_div_cnt <= stop_div_cnt + 8'd1;
254
+ else
255
+ # simulation_delay stop_div_cnt <= 8'd0;
256
+ end
257
+
258
+ // 停止位进程独热码
259
+ always @(posedge clk or negedge resetn)
260
+ begin
261
+ if(~resetn)
262
+ stop_stage_onehot <= 6'b00_00_01;
263
+ else if((i2c_if_ctrler_sts == CTRLER_STS_STOP) & (mode_latched == MODE_WITH_STOP) &
264
+ ((stop_stage_onehot[0] | stop_stage_onehot[2]) ? (stop_div_cnt == i2c_scl_div_rate):1'b1))
265
+ # simulation_delay stop_stage_onehot <= {stop_stage_onehot[4:0], stop_stage_onehot[5]};
266
+ end
267
+
268
+ /** I2C时钟分频 **/
269
+ reg[7:0] i2c_scl_div_cnt; // I2C时钟分频计数器
270
+ wire i2c_scl_div_cnt_rst; // I2C时钟分频计数器回零指示
271
+
272
+ assign i2c_scl_div_cnt_rst = i2c_scl_div_cnt == i2c_scl_div_rate;
273
+
274
+ // 待发送数据移位标志
275
+ always @(posedge clk or negedge resetn)
276
+ begin
277
+ if(~resetn)
278
+ shift_send_bit <= 1'b0;
279
+ else
280
+ # simulation_delay shift_send_bit <= scl_o & (i2c_if_ctrler_sts == CTRLER_STS_DATA) & i2c_scl_div_cnt_rst;
281
+ end
282
+ // 待接收数据采样标志
283
+ always @(posedge clk or negedge resetn)
284
+ begin
285
+ if(~resetn)
286
+ sample_recv_bit <= 1'b0;
287
+ else
288
+ # simulation_delay sample_recv_bit <= (~scl_o) & (i2c_if_ctrler_sts == CTRLER_STS_DATA) & i2c_scl_div_cnt_rst;
289
+ end
290
+
291
+ // I2C时钟分频计数器
292
+ always @(posedge clk)
293
+ begin
294
+ if(i2c_if_ctrler_sts == CTRLER_STS_DATA)
295
+ # simulation_delay i2c_scl_div_cnt <= (i2c_scl_div_cnt == i2c_scl_div_rate) ? 8'd0:(i2c_scl_div_cnt + 8'd1);
296
+ else
297
+ # simulation_delay i2c_scl_div_cnt <= 8'd0;
298
+ end
299
+
300
+ /** I2C主接口 **/
301
+ reg scl_o_reg; // SCL输出
302
+ reg sda_t_reg; // SDA方向
303
+ reg sda_o_reg; // SDA输出
304
+
305
+ assign scl_t = 1'b0;
306
+ assign scl_o = scl_o_reg;
307
+ assign sda_t = sda_t_reg;
308
+ assign sda_o = sda_o_reg;
309
+
310
+ // SCL输出
311
+ always @(posedge clk or negedge resetn)
312
+ begin
313
+ if(~resetn)
314
+ scl_o_reg <= 1'b1;
315
+ else if(((i2c_if_ctrler_sts == CTRLER_STS_DATA) & i2c_scl_div_cnt_rst) |
316
+ ((i2c_if_ctrler_sts == CTRLER_STS_RESP) & (resp_stage_onehot[1] | resp_stage_onehot[3])) |
317
+ ((i2c_if_ctrler_sts == CTRLER_STS_STOP) & (mode_latched == MODE_WITH_STOP) & (stop_stage_onehot[1] | stop_stage_onehot[3])))
318
+ begin
319
+ # simulation_delay scl_o_reg <= ~scl_o_reg;
320
+ end
321
+ end
322
+
323
+ // SDA方向(1'b1为输入, 1'b0为输出)
324
+ always @(posedge clk or negedge resetn)
325
+ begin
326
+ if(~resetn)
327
+ sda_t_reg <= 1'b1;
328
+ else if(((i2c_if_ctrler_sts == CTRLER_STS_START) & (mode_latched == MODE_WITH_START)) |
329
+ ((i2c_if_ctrler_sts == CTRLER_STS_DATA) & shift_send_bit) |
330
+ ((i2c_if_ctrler_sts == CTRLER_STS_RESP) & resp_stage_onehot[2]) |
331
+ ((i2c_if_ctrler_sts == CTRLER_STS_STOP) & stop_stage_onehot[2]))
332
+ begin
333
+ # simulation_delay sda_t_reg <=
334
+ (i2c_if_ctrler_sts == CTRLER_STS_START) ? 1'b0:
335
+ (i2c_if_ctrler_sts == CTRLER_STS_DATA) ? direction_latched:
336
+ (i2c_if_ctrler_sts == CTRLER_STS_RESP) ? (~direction_latched):
337
+ 1'b0;
338
+ end
339
+ end
340
+ // SDA输出
341
+ always @(posedge clk or negedge resetn)
342
+ begin
343
+ if(~resetn)
344
+ sda_o_reg <= 1'b1;
345
+ else if(((i2c_if_ctrler_sts == CTRLER_STS_START) & (mode_latched == MODE_WITH_START)) |
346
+ ((i2c_if_ctrler_sts == CTRLER_STS_DATA) & (~direction_latched) & shift_send_bit) |
347
+ ((i2c_if_ctrler_sts == CTRLER_STS_RESP) & resp_stage_onehot[2] & direction_latched) |
348
+ ((i2c_if_ctrler_sts == CTRLER_STS_STOP) & (stop_stage_onehot[2] | stop_stage_onehot[4])))
349
+ begin
350
+ # simulation_delay sda_o_reg <=
351
+ (i2c_if_ctrler_sts == CTRLER_STS_START) ? 1'b0:
352
+ (i2c_if_ctrler_sts == CTRLER_STS_DATA) ? send_byte_buffer[7]:
353
+ (i2c_if_ctrler_sts == CTRLER_STS_RESP) ? (mode_latched == MODE_WITH_STOP):
354
+ stop_stage_onehot[4];
355
+ end
356
+ end
357
+
358
+ endmodule
CutestPanda_Opensoc/102_apb_i2c/rtl/itr_generator.v ADDED
@@ -0,0 +1,104 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ MIT License
3
+
4
+ Copyright (c) 2024 Panda, 2257691535@qq.com
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
23
+ */
24
+
25
+ `timescale 1ns / 1ps
26
+ /********************************************************************
27
+ 本模块: 中断信号发生器
28
+
29
+ 描述:
30
+ 将原始的中断脉冲拓展为一定脉宽的中断信号
31
+
32
+ 注意:
33
+ 中断脉宽必须 >= 2
34
+
35
+ 协议:
36
+
37
+
38
+ 作者: 陈家耀
39
+ 日期: 2023/11/08
40
+ ********************************************************************/
41
+
42
+
43
+ module itr_generator #(
44
+ parameter integer pulse_w = 100, // 中断脉宽(必须>=1)
45
+ parameter real simulation_delay = 1 // 仿真延时
46
+ )(
47
+ // 时钟和复位
48
+ input wire clk,
49
+ input wire rst_n,
50
+
51
+ input wire itr_org, // 原始中断输入
52
+ output wire itr // 中断输出
53
+ );
54
+
55
+ // 计算log2(bit_depth)
56
+ function integer clogb2 (input integer bit_depth);
57
+ integer temp;
58
+ begin
59
+ temp = bit_depth;
60
+ for(clogb2 = -1;temp > 0;clogb2 = clogb2 + 1)
61
+ temp = temp >> 1;
62
+ end
63
+ endfunction
64
+
65
+ // 产生中断信号
66
+ reg itr_reg; // 中断信号
67
+ reg[clogb2(pulse_w - 1):0] itr_cnt; // 中断计数器
68
+
69
+ assign itr = itr_reg;
70
+
71
+ generate
72
+ if(pulse_w == 1)
73
+ begin
74
+ always @(posedge clk or negedge rst_n)
75
+ begin
76
+ if(~rst_n)
77
+ itr_reg <= 1'b0;
78
+ else
79
+ # simulation_delay itr_reg <= itr_org;
80
+ end
81
+ end
82
+ else
83
+ begin
84
+ always @(posedge clk or negedge rst_n)
85
+ begin
86
+ if(~rst_n)
87
+ itr_reg <= 1'b0;
88
+ else
89
+ begin
90
+ # simulation_delay;
91
+
92
+ if(~itr_reg) // 等待原始中断脉冲
93
+ itr_reg <= itr_org;
94
+ else // 等待计数完成
95
+ itr_reg <= itr_cnt != pulse_w - 1;
96
+ end
97
+ end
98
+ end
99
+ endgenerate
100
+
101
+ always @(posedge clk)
102
+ # simulation_delay itr_cnt <= (~itr_reg) ? 0:(itr_cnt + 1);
103
+
104
+ endmodule
CutestPanda_Opensoc/102_apb_i2c/rtl/ram_fifo_wrapper.v ADDED
@@ -0,0 +1,220 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ MIT License
3
+
4
+ Copyright (c) 2024 Panda, 2257691535@qq.com
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
23
+ */
24
+
25
+ `timescale 1ns / 1ps
26
+ /********************************************************************
27
+ 本模块: 基于lutram或bram的同步fifo
28
+
29
+ 描述:
30
+ 全流水的高性能同步fifo
31
+ 基于lutram或bram
32
+ 支持first word fall through特性(READ LA = 0)
33
+ 可选的固定阈值将满/将空信号
34
+ 可选的存储计数输出
35
+
36
+ 注意:
37
+ 将满信号当存储计数 >= almost_full_th时有效
38
+ 将空信号当存储计数 <= almost_empty_th时有效
39
+ almost_full_th和almost_empty_th必须在[1, fifo_depth-1]范围内
40
+ 本模块用于fpga, 子模块fifo_based_on_ram可用于asic设计
41
+ 寄存器fifo请用子模块fifo_based_on_regs
42
+
43
+ 协议:
44
+ FIFO WRITE/READ
45
+
46
+ 作者: 陈家耀
47
+ 日期: 2023/10/29
48
+ ********************************************************************/
49
+
50
+
51
+ module ram_fifo_wrapper #(
52
+ parameter fwft_mode = "true", // 是否启用first word fall through特性
53
+ parameter ram_type = "lutram", // RAM类型(lutram|bram)
54
+ parameter en_bram_reg = "false", // 是否启用BRAM输出寄存器
55
+ parameter integer fifo_depth = 32, // fifo深度(必须为2|4|8|16|...)
56
+ parameter integer fifo_data_width = 32, // fifo位宽
57
+ parameter full_assert_polarity = "low", // 满信号有效极性(low|high)
58
+ parameter empty_assert_polarity = "low", // 空信号有效极性(low|high)
59
+ parameter almost_full_assert_polarity = "no", // 将满信号有效极性(low|high|no)
60
+ parameter almost_empty_assert_polarity = "no", // 将空信号有效极性(low|high|no)
61
+ parameter en_data_cnt = "false", // 是否启用存储计数器
62
+ parameter integer almost_full_th = 20, // fifo将满阈值
63
+ parameter integer almost_empty_th = 5, // fifo将空阈值
64
+ parameter real simulation_delay = 1 // 仿真延时
65
+ )(
66
+ // 时钟和复位
67
+ input wire clk,
68
+ input wire rst_n,
69
+
70
+ // FIFO WRITE(fifo写端口)
71
+ input wire fifo_wen,
72
+ input wire[fifo_data_width-1:0] fifo_din,
73
+ output wire fifo_full,
74
+ output wire fifo_full_n,
75
+ output wire fifo_almost_full,
76
+ output wire fifo_almost_full_n,
77
+
78
+ // FIFO READ(fifo读端口)
79
+ input wire fifo_ren,
80
+ output wire[fifo_data_width-1:0] fifo_dout,
81
+ output wire fifo_empty,
82
+ output wire fifo_empty_n,
83
+ output wire fifo_almost_empty,
84
+ output wire fifo_almost_empty_n,
85
+
86
+ // 存储计数
87
+ output wire[clogb2(fifo_depth):0] data_cnt
88
+ );
89
+
90
+ // 计算log2(bit_depth)
91
+ function integer clogb2 (input integer bit_depth);
92
+ integer temp;
93
+ begin
94
+ temp = bit_depth;
95
+ for(clogb2 = -1;temp > 0;clogb2 = clogb2 + 1)
96
+ temp = temp >> 1;
97
+ end
98
+ endfunction
99
+
100
+ wire fifo_full_w;
101
+ wire fifo_full_n_w;
102
+ wire fifo_almost_full_w;
103
+ wire fifo_almost_full_n_w;
104
+ wire fifo_empty_w;
105
+ wire fifo_empty_n_w;
106
+ wire fifo_almost_empty_w;
107
+ wire fifo_almost_empty_n_w;
108
+ wire[clogb2(fifo_depth):0] data_cnt_w;
109
+
110
+ generate
111
+ if(full_assert_polarity == "low")
112
+ assign fifo_full_n = fifo_full_n_w;
113
+ else
114
+ assign fifo_full = fifo_full_w;
115
+
116
+ if(almost_full_assert_polarity == "low")
117
+ assign fifo_almost_full_n = fifo_almost_full_n_w;
118
+ else if(almost_full_assert_polarity == "high")
119
+ assign fifo_almost_full = fifo_almost_full_w;
120
+
121
+ if(empty_assert_polarity == "low")
122
+ assign fifo_empty_n = fifo_empty_n_w;
123
+ else
124
+ assign fifo_empty = fifo_empty_w;
125
+
126
+ if(almost_empty_assert_polarity == "low")
127
+ assign fifo_almost_empty_n = fifo_almost_empty_n_w;
128
+ else if(almost_empty_assert_polarity == "high")
129
+ assign fifo_almost_empty = fifo_almost_empty_w;
130
+
131
+ if(en_data_cnt == "true")
132
+ assign data_cnt = data_cnt_w;
133
+
134
+ if(ram_type == "lutram")
135
+ begin
136
+ fifo_based_on_lutram #(
137
+ .fwft_mode(fwft_mode),
138
+ .fifo_depth(fifo_depth),
139
+ .fifo_data_width(fifo_data_width),
140
+ .almost_full_th(almost_full_th),
141
+ .almost_empty_th(almost_empty_th),
142
+ .simulation_delay(simulation_delay)
143
+ )fifo(
144
+ .clk(clk),
145
+ .rst_n(rst_n),
146
+ .fifo_wen(fifo_wen),
147
+ .fifo_din(fifo_din),
148
+ .fifo_full(fifo_full_w),
149
+ .fifo_full_n(fifo_full_n_w),
150
+ .fifo_almost_full(fifo_almost_full_w),
151
+ .fifo_almost_full_n(fifo_almost_full_n_w),
152
+ .fifo_ren(fifo_ren),
153
+ .fifo_dout(fifo_dout),
154
+ .fifo_empty(fifo_empty_w),
155
+ .fifo_empty_n(fifo_empty_n_w),
156
+ .fifo_almost_empty(fifo_almost_empty_w),
157
+ .fifo_almost_empty_n(fifo_almost_empty_n_w),
158
+ .data_cnt(data_cnt_w)
159
+ );
160
+ end
161
+ else
162
+ begin
163
+ wire ram_wen_a;
164
+ wire[clogb2(fifo_depth-1):0] ram_addr_a;
165
+ wire[fifo_data_width-1:0] ram_din_a;
166
+ wire ram_ren_b;
167
+ wire[clogb2(fifo_depth-1):0] ram_addr_b;
168
+ wire[fifo_data_width-1:0] ram_dout_b;
169
+
170
+ fifo_based_on_ram #(
171
+ .fwft_mode(fwft_mode),
172
+ .ram_read_la((en_bram_reg == "true") ? 2:1),
173
+ .fifo_depth(fifo_depth),
174
+ .fifo_data_width(fifo_data_width),
175
+ .almost_full_th(almost_full_th),
176
+ .almost_empty_th(almost_empty_th),
177
+ .simulation_delay(simulation_delay)
178
+ )fifo_ctrler(
179
+ .clk(clk),
180
+ .rst_n(rst_n),
181
+ .fifo_wen(fifo_wen),
182
+ .fifo_din(fifo_din),
183
+ .fifo_full(fifo_full_w),
184
+ .fifo_full_n(fifo_full_n_w),
185
+ .fifo_almost_full(fifo_almost_full_w),
186
+ .fifo_almost_full_n(fifo_almost_full_n_w),
187
+ .fifo_ren(fifo_ren),
188
+ .fifo_dout(fifo_dout),
189
+ .fifo_empty(fifo_empty_w),
190
+ .fifo_empty_n(fifo_empty_n_w),
191
+ .fifo_almost_empty(fifo_almost_empty_w),
192
+ .fifo_almost_empty_n(fifo_almost_empty_n_w),
193
+ .ram_wen(ram_wen_a),
194
+ .ram_w_addr(ram_addr_a),
195
+ .ram_din(ram_din_a),
196
+ .ram_ren(ram_ren_b),
197
+ .ram_r_addr(ram_addr_b),
198
+ .ram_dout(ram_dout_b),
199
+ .data_cnt(data_cnt_w)
200
+ );
201
+
202
+ bram_simple_dual_port #(
203
+ .style((en_bram_reg == "true") ? "HIGH_PERFORMANCE":"LOW_LATENCY"),
204
+ .mem_width(fifo_data_width),
205
+ .mem_depth(fifo_depth),
206
+ .INIT_FILE("no_init"),
207
+ .simulation_delay(simulation_delay)
208
+ )fifo_ram(
209
+ .clk(clk),
210
+ .wen_a(ram_wen_a),
211
+ .addr_a(ram_addr_a),
212
+ .din_a(ram_din_a),
213
+ .ren_b(ram_ren_b),
214
+ .addr_b(ram_addr_b),
215
+ .dout_b(ram_dout_b)
216
+ );
217
+ end
218
+ endgenerate
219
+
220
+ endmodule
CutestPanda_Opensoc/102_apb_i2c/rtl/regs_if_for_i2c.v ADDED
@@ -0,0 +1,307 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ MIT License
3
+
4
+ Copyright (c) 2024 Panda, 2257691535@qq.com
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
23
+ */
24
+
25
+ `timescale 1ns / 1ps
26
+ /********************************************************************
27
+ 本模块: APB-I2C的寄存器接口
28
+
29
+ 描述:
30
+ 寄存器->
31
+ 偏移量 | 含义 | 读写特性 | 备注
32
+ 0x00 0:发送fifo是否满 R
33
+ 1:发送fifo写使能 W 写该寄存器且该位为1'b1时产生发送fifo写使能
34
+ 10~2:发送fifo写数据 W {last(1bit), data(8bit)}
35
+ 16:接收fifo是否空 R
36
+ 17:接收fifo读使能 W 写该寄存器且该位为1'b1时产生接收fifo读使能
37
+ 25~18:接收fifo读数据 R
38
+ 0x04 0:I2C全局中断使能 W
39
+ 8:I2C发送指定字节数中断使能 W
40
+ 9:I2C从机响应错误中断使能 W
41
+ 10:I2C接收指定字节数中断使能 W
42
+ 11:I2C接收溢出中断使能 W
43
+ 0x08 7~0:I2C发送中断字节数阈值 W 发送字节数 > 阈值时发生中断
44
+ 15~8:I2C接收中断字节数阈值 W 接收字节数 > 阈值时发生中断
45
+ 23~16:I2C时钟分频系数 W 分频数 = (分频系数 + 1) * 2
46
+ 分频系数应>=1
47
+ 0x0C 0:I2C全局中断标志 RWC 请在中断服务函数中清除中断标志
48
+ 1:I2C发送指定字节数中断标志 R
49
+ 2:I2C从机响应错误中断标志 R
50
+ 3:I2C接收指定字节数中断标志 R
51
+ 4:I2C接收溢出中断标志 R
52
+ 19~8:I2C发送字节数 RWC 每当发送一个I2C数据包后更新
53
+ 31~20:I2C接收字节数 RWC 每当接收一个I2C数据包后更新
54
+
55
+ 注意:
56
+ I2C发送/接收指定字节数中断每当发送/接收一个I2C数据包后判断
57
+ 每个I2C数据包不能超过15字节
58
+
59
+ 协议:
60
+ APB SLAVE
61
+
62
+ 作者: 陈家耀
63
+ 日期: 2024/06/14
64
+ ********************************************************************/
65
+
66
+
67
+ module regs_if_for_i2c #(
68
+ parameter real simulation_delay = 1 // 仿真延时
69
+ )(
70
+ // 时钟和复位
71
+ input wire clk,
72
+ input wire resetn,
73
+
74
+ // APB从机接口
75
+ input wire[31:0] paddr,
76
+ input wire psel,
77
+ input wire penable,
78
+ input wire pwrite,
79
+ input wire[31:0] pwdata,
80
+ output wire pready_out, // const -> 1'b1
81
+ output wire[31:0] prdata_out,
82
+ output wire pslverr_out, // const -> 1'b0
83
+
84
+ // 发送fifo写端口
85
+ output wire tx_fifo_wen,
86
+ output wire[8:0] tx_fifo_din,
87
+ input wire tx_fifo_full,
88
+ // 接收fifo读端口
89
+ output wire rx_fifo_ren,
90
+ input wire[7:0] rx_fifo_dout,
91
+ input wire rx_fifo_empty,
92
+
93
+ // I2C时钟分频系数
94
+ output wire[7:0] i2c_scl_div_rate,
95
+
96
+ // I2C发送完成指示
97
+ input wire i2c_tx_done,
98
+ input wire[3:0] i2c_tx_bytes_n,
99
+ // I2C接收完成指示
100
+ input wire i2c_rx_done,
101
+ input wire[3:0] i2c_rx_bytes_n,
102
+ // I2C从机响应错误
103
+ input wire i2c_slave_resp_err,
104
+ // I2C接收溢出
105
+ input wire i2c_rx_overflow,
106
+
107
+ // 中断信号
108
+ output wire itr
109
+ );
110
+
111
+ /** APB写寄存器和中断处理 **/
112
+ // 0x00
113
+ reg tx_fifo_wen_reg; // 发送fifo写使能
114
+ reg[8:0] tx_fifo_din_regs; // 发送fifo写数据
115
+ reg rx_fifo_ren_reg; // 接收fifo读使能
116
+ // 0x04
117
+ reg global_itr_en; // 全局中断使能
118
+ reg i2c_tx_reach_bytes_n_itr_en; // I2C发送指定字节数中断使能
119
+ reg i2c_slave_resp_err_itr_en; // I2C从机响应错误中断使能
120
+ reg i2c_rx_reach_bytes_n_itr_en; // I2C接收指定字节数中���使能
121
+ reg i2c_rx_overflow_itr_en; // I2C接收溢出中断使能
122
+ // 0x08
123
+ reg[7:0] i2c_tx_bytes_n_th; // I2C发送中断字节数阈值
124
+ reg[7:0] i2c_rx_bytes_n_th; // I2C接收中断字节数阈值
125
+ reg[7:0] i2c_scl_div_rate_regs; // I2C时钟分频系数
126
+ // 0x0C
127
+ reg global_itr_flag; // 全局中断标志
128
+ reg[3:0] sub_itr_flag; // 子中断标志
129
+ reg[11:0] i2c_bytes_n_sent; // I2C发送字节数
130
+ reg[11:0] i2c_bytes_n_rev; // I2C接收字节数
131
+ // 中断处理
132
+ reg i2c_tx_done_d; // 延迟1clk的I2C发送完成指示
133
+ reg i2c_rx_done_d; // 延迟1clk的I2C发送完成指示
134
+ reg i2c_tx_reach_bytes_n_itr_req; // I2C发送指定字节数中断请求
135
+ wire i2c_slave_resp_err_itr_req; // I2C从机响应错误中断请求
136
+ reg i2c_rx_reach_bytes_n_itr_req; // I2C接收指定字节数中断请求
137
+ wire i2c_rx_overflow_itr_req; // I2C接收溢出中断请求
138
+ wire[3:0] org_itr_req_vec; // 原始中断请求向量
139
+ wire global_itr_req; // 总中断请求
140
+
141
+ assign tx_fifo_wen = tx_fifo_wen_reg;
142
+ assign tx_fifo_din = tx_fifo_din_regs;
143
+ assign rx_fifo_ren = rx_fifo_ren_reg;
144
+
145
+ assign i2c_scl_div_rate = i2c_scl_div_rate_regs;
146
+
147
+ assign i2c_slave_resp_err_itr_req = i2c_slave_resp_err;
148
+ assign i2c_rx_overflow_itr_req = i2c_rx_overflow;
149
+
150
+ assign org_itr_req_vec = {i2c_rx_overflow_itr_req, i2c_rx_reach_bytes_n_itr_req, i2c_slave_resp_err_itr_req, i2c_tx_reach_bytes_n_itr_req} &
151
+ {i2c_rx_overflow_itr_en, i2c_rx_reach_bytes_n_itr_en, i2c_slave_resp_err_itr_en, i2c_tx_reach_bytes_n_itr_en};
152
+ assign global_itr_req = (|org_itr_req_vec) & global_itr_en & (~global_itr_flag);
153
+
154
+ // 发送fifo写使能
155
+ always @(posedge clk or negedge resetn)
156
+ begin
157
+ if(~resetn)
158
+ tx_fifo_wen_reg <= 1'b0;
159
+ else
160
+ # simulation_delay tx_fifo_wen_reg <= psel & penable & pwrite & (paddr[3:2] == 2'd0) & pwdata[1];
161
+ end
162
+ // 发送fifo写数据
163
+ always @(posedge clk)
164
+ begin
165
+ if(psel & penable & pwrite & (paddr[3:2] == 2'd0))
166
+ # simulation_delay tx_fifo_din_regs <= pwdata[10:2];
167
+ end
168
+ // 接收fifo读使能
169
+ always @(posedge clk or negedge resetn)
170
+ begin
171
+ if(~resetn)
172
+ rx_fifo_ren_reg <= 1'b0;
173
+ else
174
+ # simulation_delay rx_fifo_ren_reg <= psel & penable & pwrite & (paddr[3:2] == 2'd0) & pwdata[17];
175
+ end
176
+
177
+ // 全局中断使能
178
+ // I2C发送指定字节数中断使能
179
+ // I2C从机响应错误中断使能
180
+ // I2C接收指定字节数中断使能
181
+ // I2C接收溢出中断使能
182
+ always @(posedge clk or negedge resetn)
183
+ begin
184
+ if(~resetn)
185
+ {i2c_rx_overflow_itr_en, i2c_rx_reach_bytes_n_itr_en, i2c_slave_resp_err_itr_en, i2c_tx_reach_bytes_n_itr_en, global_itr_en} <= 5'd0;
186
+ else if(psel & penable & pwrite & (paddr[3:2] == 2'd1))
187
+ # simulation_delay {i2c_rx_overflow_itr_en, i2c_rx_reach_bytes_n_itr_en, i2c_slave_resp_err_itr_en, i2c_tx_reach_bytes_n_itr_en, global_itr_en} <=
188
+ {pwdata[11:8], pwdata[0]};
189
+ end
190
+
191
+ // I2C发送中断字节数阈值
192
+ // I2C接收中断字节数阈值
193
+ // I2C时钟分频系数
194
+ always @(posedge clk)
195
+ begin
196
+ if(psel & penable & pwrite & (paddr[3:2] == 2'd2))
197
+ # simulation_delay {i2c_scl_div_rate_regs, i2c_rx_bytes_n_th, i2c_tx_bytes_n_th} <= pwdata[23:0];
198
+ end
199
+
200
+ // 全局中断标志
201
+ always @(posedge clk or negedge resetn)
202
+ begin
203
+ if(~resetn)
204
+ global_itr_flag <= 1'b0;
205
+ else if(psel & penable & pwrite & (paddr[3:2] == 2'd3))
206
+ # simulation_delay global_itr_flag <= 1'b0;
207
+ else if(~global_itr_flag)
208
+ # simulation_delay global_itr_flag <= global_itr_req;
209
+ end
210
+ // 子中断标志
211
+ always @(posedge clk)
212
+ begin
213
+ if(global_itr_req)
214
+ # simulation_delay sub_itr_flag <= org_itr_req_vec;
215
+ end
216
+ // I2C发送字节数
217
+ always @(posedge clk or negedge resetn)
218
+ begin
219
+ if(~resetn)
220
+ i2c_bytes_n_sent <= 12'd0;
221
+ else if(psel & penable & pwrite & (paddr[3:2] == 2'd3))
222
+ # simulation_delay i2c_bytes_n_sent <= 12'd0;
223
+ else if(i2c_tx_done)
224
+ # simulation_delay i2c_bytes_n_sent <= i2c_bytes_n_sent + i2c_tx_bytes_n;
225
+ end
226
+ // I2C接收字节数
227
+ always @(posedge clk or negedge resetn)
228
+ begin
229
+ if(~resetn)
230
+ i2c_bytes_n_rev <= 12'd0;
231
+ else if(psel & penable & pwrite & (paddr[3:2] == 2'd3))
232
+ # simulation_delay i2c_bytes_n_rev <= 12'd0;
233
+ else if(i2c_rx_done)
234
+ # simulation_delay i2c_bytes_n_rev <= i2c_bytes_n_rev + i2c_rx_bytes_n;
235
+ end
236
+
237
+ // 延迟1clk的I2C发送完成指示
238
+ always @(posedge clk or negedge resetn)
239
+ begin
240
+ if(~resetn)
241
+ i2c_tx_done_d <= 1'b0;
242
+ else
243
+ # simulation_delay i2c_tx_done_d <= i2c_tx_done;
244
+ end
245
+ // 延迟1clk的I2C发送完成指示
246
+ always @(posedge clk or negedge resetn)
247
+ begin
248
+ if(~resetn)
249
+ i2c_rx_done_d <= 1'b0;
250
+ else
251
+ # simulation_delay i2c_rx_done_d <= i2c_rx_done;
252
+ end
253
+ // I2C发送指定字节数中断请求
254
+ always @(posedge clk or negedge resetn)
255
+ begin
256
+ if(~resetn)
257
+ i2c_tx_reach_bytes_n_itr_req <= 1'b0;
258
+ else
259
+ # simulation_delay i2c_tx_reach_bytes_n_itr_req <= i2c_tx_done_d & (i2c_bytes_n_sent > i2c_tx_bytes_n_th);
260
+ end
261
+ // I2C接收指定字节数中断请求
262
+ always @(posedge clk or negedge resetn)
263
+ begin
264
+ if(~resetn)
265
+ i2c_rx_reach_bytes_n_itr_req <= 1'b0;
266
+ else
267
+ # simulation_delay i2c_rx_reach_bytes_n_itr_req <= i2c_rx_done_d & (i2c_bytes_n_rev > i2c_rx_bytes_n_th);
268
+ end
269
+
270
+ // 中断发生器
271
+ itr_generator #(
272
+ .pulse_w(10),
273
+ .simulation_delay(simulation_delay)
274
+ )itr_generator_u(
275
+ .clk(clk),
276
+ .rst_n(resetn),
277
+
278
+ .itr_org(global_itr_req),
279
+
280
+ .itr(itr)
281
+ );
282
+
283
+ /** APB读寄存器 **/
284
+ reg[31:0] prdata_out_regs; // APB读数据输出
285
+
286
+ assign pready_out = 1'b1;
287
+ assign prdata_out = prdata_out_regs;
288
+ assign pslverr_out = 1'b0;
289
+
290
+ // APB读数据输出
291
+ always @(posedge clk)
292
+ begin
293
+ if(psel & (~pwrite))
294
+ begin
295
+ # simulation_delay;
296
+
297
+ case(paddr[3:2])
298
+ 2'd0: prdata_out_regs <= {6'dx, rx_fifo_dout, 1'bx, rx_fifo_empty, 15'dx, tx_fifo_full};
299
+ 2'd1: prdata_out_regs <= 32'dx;
300
+ 2'd2: prdata_out_regs <= 32'dx;
301
+ 2'd3: prdata_out_regs <= {i2c_bytes_n_rev, i2c_bytes_n_sent, 3'dx, sub_itr_flag, global_itr_flag};
302
+ default: prdata_out_regs <= 32'dx;
303
+ endcase
304
+ end
305
+ end
306
+
307
+ endmodule
CutestPanda_Opensoc/103_generic_fsmc_ctrler/firmware/examples/fsmc_example.c ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /************************************************************************************************************************
2
+ FSMC������ʾ������
3
+ @brief ����FSMC������ʵ���ⲿSRAM��д
4
+ @date 2024/08/29
5
+ @author �¼�ҫ
6
+ @eidt 2024/08/29 1.00 �����˵�һ����ʽ�汾
7
+ ************************************************************************************************************************/
8
+
9
+ #include "../generic_fsmc_ctrler.h"
10
+
11
+ ////////////////////////////////////////////////////////////////////////////////////////////////////////////
12
+
13
+ #define BASEADDR_FSMC 0x60000000 // FSMC�������ַ
14
+
15
+ ////////////////////////////////////////////////////////////////////////////////////////////////////////////
16
+
17
+ static Fsmc fsmc; // FSMC����������ṹ��
18
+
19
+ ////////////////////////////////////////////////////////////////////////////////////////////////////////////
20
+
21
+ void fsmc_example(void){
22
+ init_fsmc(BASEADDR_FSMC, &fsmc); // ��ʼ��FSMC����
23
+
24
+ /*
25
+ ����FSMC������ʱ�����
26
+
27
+ ��ַ���������� = 10
28
+ ���ݽ��������� = 10
29
+ ���ݱ��������� = 10
30
+ */
31
+ fsmc_cfg_rt_pars(&fsmc, 9, 9, 9);
32
+
33
+ /** д�ⲿSRAM **/
34
+ for(uint32_t i = 0;i < 50;i++){
35
+ fsmc_write_16(&fsmc, i << 1, i + 1);
36
+ }
37
+
38
+ /** ���ⲿSRAM **/
39
+ uint8_t verify_ok = 1;
40
+
41
+ for(uint32_t i = 0;i < 50;i++){
42
+ uint16_t d = fsmc_read_16(&fsmc, i << 1);
43
+
44
+ if(d != (i + 1)){
45
+ verify_ok = 0;
46
+
47
+ break;
48
+ }
49
+ }
50
+
51
+ /** ��֤��д����һ���� **/
52
+ if(verify_ok){
53
+ // ��д����һ��
54
+ // ...
55
+ }else{
56
+ // ��д���ݲ�һ��
57
+ // ...
58
+ }
59
+
60
+ while(1);
61
+ }
CutestPanda_Opensoc/103_generic_fsmc_ctrler/firmware/generic_fsmc_ctrler.c ADDED
@@ -0,0 +1,99 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /************************************************************************************************************************
2
+ ͨ��FSMC����������(��Դ�ļ�)
3
+ @brief ͨ��FSMC����������
4
+ @date 2024/08/29
5
+ @author �¼�ҫ
6
+ @eidt 2024/08/29 1.00 �����˵�һ����ʽ�汾
7
+ ************************************************************************************************************************/
8
+
9
+ #include "generic_fsmc_ctrler.h"
10
+
11
+ ////////////////////////////////////////////////////////////////////////////////////////////////////////////
12
+
13
+ /*************************
14
+ @init
15
+ @public
16
+ @brief ��ʼ��FSMC������
17
+ @param base_addr FSMC�������ַ
18
+ fsmc FSMC����(�ṹ��ָ��)
19
+ @return none
20
+ *************************/
21
+ void init_fsmc(uint32_t base_addr, Fsmc* fsmc){
22
+ fsmc->base_addr_regs = base_addr;
23
+ fsmc->base_addr_mem = base_addr + 0x00001000;
24
+ }
25
+
26
+ /*************************
27
+ @cfg
28
+ @public
29
+ @brief ����FSMC������ʱ�����
30
+ @param fsmc FSMC����(�ṹ��ָ��)
31
+ addr_set ��ַ���������� - 1
32
+ data_set ���ݽ��������� - 1
33
+ data_hold ���ݱ��������� - 1
34
+ @return none
35
+ *************************/
36
+ void fsmc_cfg_rt_pars(Fsmc* fsmc, uint8_t addr_set, uint8_t data_set, uint8_t data_hold){
37
+ uint16_t* ptr = (uint16_t*)fsmc->base_addr_regs;
38
+
39
+ ptr[0] = ((uint16_t)addr_set) | (((uint16_t)data_set) << 8);
40
+ ptr[1] = (uint16_t)data_hold;
41
+ }
42
+
43
+ /*************************
44
+ @io
45
+ @public
46
+ @brief FSMC��������16λ����λ��д����
47
+ @param fsmc FSMC����(�ṹ��ָ��)
48
+ ofs FSMC�洢ƫ�Ƶ�ַ
49
+ data ��д�İ�������
50
+ @return none
51
+ *************************/
52
+ void fsmc_write_16(Fsmc* fsmc, uint32_t ofs, uint16_t data){
53
+ uint16_t* ptr = (uint16_t*)(fsmc->base_addr_mem + ofs);
54
+
55
+ *ptr = data;
56
+ }
57
+
58
+ /*************************
59
+ @io
60
+ @public
61
+ @brief FSMC��������8λ����λ��д����
62
+ @param fsmc FSMC����(�ṹ��ָ��)
63
+ ofs FSMC�洢ƫ�Ƶ�ַ
64
+ data ��д���ֽ�����
65
+ @return none
66
+ *************************/
67
+ void fsmc_write_8(Fsmc* fsmc, uint32_t ofs, uint8_t data){
68
+ uint16_t* ptr = (uint16_t*)(fsmc->base_addr_mem + (ofs << 1));
69
+
70
+ *ptr = (uint16_t)data;
71
+ }
72
+
73
+ /*************************
74
+ @io
75
+ @public
76
+ @brief FSMC��������16����������
77
+ @param fsmc FSMC����(�ṹ��ָ��)
78
+ ofs FSMC�洢ƫ�Ƶ�ַ
79
+ @return ��ȡ���İ�������
80
+ *************************/
81
+ uint16_t fsmc_read_16(Fsmc* fsmc, uint32_t ofs){
82
+ uint16_t* ptr = (uint16_t*)(fsmc->base_addr_mem + ofs);
83
+
84
+ return *ptr;
85
+ }
86
+
87
+ /*************************
88
+ @io
89
+ @public
90
+ @brief FSMC��������8����������
91
+ @param fsmc FSMC����(�ṹ��ָ��)
92
+ ofs FSMC�洢ƫ�Ƶ�ַ
93
+ @return ��ȡ�����ֽ�����
94
+ *************************/
95
+ uint8_t fsmc_read_8(Fsmc* fsmc, uint32_t ofs){
96
+ uint16_t* ptr = (uint16_t*)(fsmc->base_addr_mem + (ofs << 1));
97
+
98
+ return (uint8_t)(*ptr & 0x00FF);
99
+ }
CutestPanda_Opensoc/103_generic_fsmc_ctrler/firmware/generic_fsmc_ctrler.h ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #include <stdint.h>
2
+
3
+ ////////////////////////////////////////////////////////////////////////////////////////////////////////////
4
+
5
+ #ifndef __AHB_FSMC
6
+ #define __AHB_FSMC
7
+
8
+ typedef struct{
9
+ uint32_t base_addr_regs; // ���üĴ���Ƭ����ַ
10
+ uint32_t base_addr_mem; // FSMC����ַ
11
+ }Fsmc;
12
+
13
+ #endif
14
+
15
+ ////////////////////////////////////////////////////////////////////////////////////////////////////////////
16
+
17
+ void init_fsmc(uint32_t base_addr, Fsmc* fsmc); // ��ʼ��FSMC������
18
+
19
+ void fsmc_cfg_rt_pars(Fsmc* fsmc, uint8_t addr_set, uint8_t data_set, uint8_t data_hold); // ����FSMC������ʱ�����
20
+
21
+ void fsmc_write_16(Fsmc* fsmc, uint32_t ofs, uint16_t data); // FSMC��������16λ����λ��д����
22
+ void fsmc_write_8(Fsmc* fsmc, uint32_t ofs, uint8_t data); // FSMC��������8λ����λ��д����
23
+
24
+ uint16_t fsmc_read_16(Fsmc* fsmc, uint32_t ofs); // FSMC��������16����������
25
+ uint8_t fsmc_read_8(Fsmc* fsmc, uint32_t ofs); // FSMC��������8����������
CutestPanda_Opensoc/103_generic_fsmc_ctrler/rtl/ahb_fsmc.v ADDED
@@ -0,0 +1,229 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ MIT License
3
+
4
+ Copyright (c) 2024 Panda, 2257691535@qq.com
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
23
+ */
24
+
25
+ `timescale 1ns / 1ps
26
+ /********************************************************************
27
+ 本模块: AHB-FSMC
28
+
29
+ 描述:
30
+ 带AHB-Lite从接口的FSMC控制器
31
+
32
+ 突发大小固定为16位
33
+
34
+ 偏移量0x000_0000~0x000_0FFF:
35
+ 子偏移量0x00 -> 7~0: 地址建立周期数 - 1
36
+ 15~8: 数据建立周期数 - 1
37
+ 子偏移量0x02 -> 7~0: 数据保持周期数 - 1
38
+ 偏移量0x000_1000~0x3FF_FFFF:
39
+ 子偏移量0x000_0000~0x3FF_EFFF -> FSMC
40
+
41
+ 注意:
42
+ 不支持非对齐传输
43
+
44
+ 协议:
45
+ AHB SLAVE
46
+ FSMC MASTER
47
+
48
+ 作者: 陈家耀
49
+ 日期: 2024/07/18
50
+ ********************************************************************/
51
+
52
+
53
+ module ahb_fsmc #(
54
+ parameter real simulation_delay = 0 // 仿真延时
55
+ )(
56
+ // 时钟和复位
57
+ input wire hclk,
58
+ input wire hresetn,
59
+
60
+ // AHB-Lite SLAVE
61
+ input wire[31:0] s_ahb_haddr,
62
+ input wire[2:0] s_ahb_hburst, // ignored(assumed to be 3'b000, i.e. SINGLE)
63
+ input wire[3:0] s_ahb_hprot, // ignored
64
+ output wire[31:0] s_ahb_hrdata,
65
+ input wire s_ahb_hready_in,
66
+ output wire s_ahb_hready_out,
67
+ output wire s_ahb_hresp, // const -> 1'b0
68
+ input wire[2:0] s_ahb_hsize, // ignored(assumed to be 3'b001)
69
+ input wire[1:0] s_ahb_htrans, // only 2'b00(IDLE) and 2'b10(NONSEQ) are supported
70
+ input wire[31:0] s_ahb_hwdata,
71
+ input wire[3:0] s_ahb_hwstrb,
72
+ input wire s_ahb_hwrite,
73
+ input wire s_ahb_hsel,
74
+
75
+ // FSMC MASTER
76
+ output wire[1:0] fsmc_nbl, // 数据掩码
77
+ output wire[25:0] fsmc_addr, // 地址线
78
+ output wire fsmc_nwe, // 写入使能
79
+ output wire fsmc_noe, // 输出使能(读使能)
80
+ output wire fsmc_ne, // 片选信号
81
+ // 数据线(1'b1 -> 输入, 1'b0 -> 输出)
82
+ input wire[15:0] fsmc_data_i,
83
+ output wire[15:0] fsmc_data_o,
84
+ output wire[15:0] fsmc_data_t
85
+ );
86
+
87
+ /** FSMC控制器 **/
88
+ // AHB传输开始指示
89
+ wire ahb_trans_start;
90
+ // 运行时参数
91
+ reg to_upd_rt_pars_0; // 更新第0组运行时参数(指示)
92
+ reg to_upd_rt_pars_1; // 更新第1组运行时参数(指示)
93
+ reg[7:0] addr_set; // 地址建立周期数 - 1
94
+ reg[7:0] data_set; // 数据建立周期数 - 1
95
+ reg[7:0] data_hold; // 数据保持周期数 - 1
96
+ // 流程控制
97
+ reg fsmc_ctrler_start;
98
+ wire fsmc_ctrler_done;
99
+ // 传输参数
100
+ wire[15:0] wdata; // 写数据
101
+ wire[1:0] data_mask; // 字节使能掩码
102
+ wire[25:0] trans_addr; // 传输地址
103
+ wire is_rd; // 是否读传输
104
+ // 读数据输出
105
+ wire[15:0] m_axis_rd_data;
106
+ wire m_axis_rd_valid;
107
+
108
+ assign ahb_trans_start = s_ahb_hready_in & s_ahb_htrans[1] & s_ahb_hsel;
109
+
110
+ // 更新第0组运行时参数(指示)
111
+ always @(posedge hclk or negedge hresetn)
112
+ begin
113
+ if(~hresetn)
114
+ to_upd_rt_pars_0 <= 1'b0;
115
+ else
116
+ # simulation_delay to_upd_rt_pars_0 <= ahb_trans_start & (s_ahb_haddr[25:12] == 14'd0) & (~s_ahb_haddr[1]);
117
+ end
118
+ // 更新第1组运行时参数(指示)
119
+ always @(posedge hclk or negedge hresetn)
120
+ begin
121
+ if(~hresetn)
122
+ to_upd_rt_pars_1 <= 1'b0;
123
+ else
124
+ # simulation_delay to_upd_rt_pars_1 <= ahb_trans_start & (s_ahb_haddr[25:12] == 14'd0) & s_ahb_haddr[1];
125
+ end
126
+
127
+ // 控制器运行时参数
128
+ always @(posedge hclk)
129
+ begin
130
+ if(to_upd_rt_pars_0)
131
+ # simulation_delay {data_set, addr_set} <= s_ahb_hwdata[15:0];
132
+ end
133
+ always @(posedge hclk)
134
+ begin
135
+ if(to_upd_rt_pars_1)
136
+ # simulation_delay data_hold <= s_ahb_hwdata[23:16];
137
+ end
138
+
139
+ fsmc_ctrler #(
140
+ .simulation_delay(simulation_delay)
141
+ )fsmc_ctrler_u(
142
+ .clk(hclk),
143
+ .rst_n(hresetn),
144
+
145
+ .ctrler_start(fsmc_ctrler_start),
146
+ .ctrler_idle(),
147
+ .ctrler_done(fsmc_ctrler_done),
148
+
149
+ .addr_set(addr_set),
150
+ .data_set(data_set),
151
+ .data_hold(data_hold),
152
+
153
+ .wdata(wdata),
154
+ .data_mask(data_mask),
155
+ .trans_addr(trans_addr),
156
+ .is_rd(is_rd),
157
+
158
+ .m_axis_rd_data(m_axis_rd_data),
159
+ .m_axis_rd_valid(m_axis_rd_valid),
160
+
161
+ .fsmc_nbl(fsmc_nbl),
162
+ .fsmc_addr(fsmc_addr),
163
+ .fsmc_nwe(fsmc_nwe),
164
+ .fsmc_noe(fsmc_noe),
165
+ .fsmc_ne(fsmc_ne),
166
+ .fsmc_data_i(fsmc_data_i),
167
+ .fsmc_data_o(fsmc_data_o),
168
+ .fsmc_data_t(fsmc_data_t)
169
+ );
170
+
171
+ /** AHB从接口 **/
172
+ reg[31:0] haddr_latched; // 锁存的AHB传输地址
173
+ reg hwrite_latched; // 锁存的AHB传输读写类型
174
+ reg[31:0] hrdata_regs; // AHB读数据
175
+ reg hready_out_reg; // AHB上一次传输完成
176
+
177
+ assign s_ahb_hrdata = hrdata_regs;
178
+ assign s_ahb_hready_out = hready_out_reg;
179
+ assign s_ahb_hresp = 1'b0;
180
+
181
+ assign wdata = haddr_latched[1] ? s_ahb_hwdata[31:16]:s_ahb_hwdata[15:0];
182
+ assign data_mask = hwrite_latched ? (haddr_latched[1] ? (~s_ahb_hwstrb[3:2]):(~s_ahb_hwstrb[1:0])):2'b00;
183
+ assign trans_addr = haddr_latched[25:0];
184
+ assign is_rd = ~hwrite_latched;
185
+
186
+ // 锁存的AHB传输地址
187
+ always @(posedge hclk)
188
+ begin
189
+ if(s_ahb_hready_in & s_ahb_htrans[1] & s_ahb_hsel)
190
+ # simulation_delay haddr_latched <= s_ahb_haddr - 32'h0000_1000;
191
+ end
192
+ // 锁存的AHB传输读写类型
193
+ always @(posedge hclk)
194
+ begin
195
+ if(s_ahb_hready_in & s_ahb_htrans[1] & s_ahb_hsel)
196
+ # simulation_delay hwrite_latched <= s_ahb_hwrite;
197
+ end
198
+
199
+ // AHB读数据
200
+ always @(posedge hclk)
201
+ begin
202
+ if(m_axis_rd_valid & haddr_latched[1])
203
+ # simulation_delay hrdata_regs[31:16] <= m_axis_rd_data;
204
+ end
205
+ always @(posedge hclk)
206
+ begin
207
+ if(m_axis_rd_valid & (~haddr_latched[1]))
208
+ # simulation_delay hrdata_regs[15:0] <= m_axis_rd_data;
209
+ end
210
+ // AHB上一次传输完成
211
+ always @(posedge hclk or negedge hresetn)
212
+ begin
213
+ if(~hresetn)
214
+ hready_out_reg <= 1'b1;
215
+ else
216
+ # simulation_delay hready_out_reg <= hready_out_reg ?
217
+ (~(ahb_trans_start & (s_ahb_haddr[25:12] != 14'd0))):fsmc_ctrler_done;
218
+ end
219
+
220
+ // FSMC控制器开始信号
221
+ always @(posedge hclk or negedge hresetn)
222
+ begin
223
+ if(~hresetn)
224
+ fsmc_ctrler_start <= 1'b0;
225
+ else
226
+ # simulation_delay fsmc_ctrler_start <= ahb_trans_start & (s_ahb_haddr[25:12] != 14'd0);
227
+ end
228
+
229
+ endmodule
CutestPanda_Opensoc/103_generic_fsmc_ctrler/rtl/axi_fsmc.v ADDED
@@ -0,0 +1,368 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ MIT License
3
+
4
+ Copyright (c) 2024 Panda, 2257691535@qq.com
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
23
+ */
24
+
25
+ `timescale 1ns / 1ps
26
+ /********************************************************************
27
+ 本模块: AXI-FSMC
28
+
29
+ 描述:
30
+ 带AXI-Lite从接口的FSMC控制器
31
+
32
+ 突发大小固定为16位
33
+
34
+ 偏移量0x000_0000~0x000_0FFF:
35
+ 子偏移量0x00 -> 7~0: 地址建立周期数 - 1
36
+ 15~8: 数据建立周期数 - 1
37
+ 子偏移量0x02 -> 7~0: 数据保持周期数 - 1
38
+ 偏移量0x000_1000~0x3FF_FFFF:
39
+ 子偏移量0x000_0000~0x3FF_EFFF -> FSMC
40
+
41
+ 注意:
42
+ 不支持非对齐传输
43
+
44
+ 协议:
45
+ AXI SLAVE
46
+ FSMC MASTER
47
+
48
+ 作者: 陈家耀
49
+ 日期: 2024/08/29
50
+ ********************************************************************/
51
+
52
+
53
+ module axi_fsmc #(
54
+ parameter real simulation_delay = 0 // 仿真延时
55
+ )(
56
+ // 时钟和复位
57
+ input wire s_axi_clk,
58
+ input wire s_axi_resetn,
59
+
60
+ // AXI-Lite SLAVE
61
+ // 读地址通道
62
+ input wire[31:0] s_axi_araddr,
63
+ input wire[2:0] s_axi_arsize, // ignored(assumed to be 3'b001)
64
+ input wire s_axi_arvalid,
65
+ output wire s_axi_arready,
66
+ // 写地址通道
67
+ input wire[31:0] s_axi_awaddr,
68
+ input wire[2:0] s_axi_awsize, // ignored(assumed to be 3'b001)
69
+ input wire s_axi_awvalid,
70
+ output wire s_axi_awready,
71
+ // 写响应通道
72
+ output wire[1:0] s_axi_bresp, // const -> 2'b00
73
+ output wire s_axi_bvalid,
74
+ input wire s_axi_bready,
75
+ // 读数据通道
76
+ output wire[31:0] s_axi_rdata,
77
+ output wire[1:0] s_axi_rresp, // const -> 2'b00
78
+ output wire s_axi_rvalid,
79
+ input wire s_axi_rready,
80
+ // 写数据通道
81
+ input wire[31:0] s_axi_wdata,
82
+ input wire[3:0] s_axi_wstrb,
83
+ input wire s_axi_wvalid,
84
+ output wire s_axi_wready,
85
+
86
+ // FSMC MASTER
87
+ output wire[1:0] fsmc_nbl, // 数据掩码
88
+ output wire[25:0] fsmc_addr, // 地址线
89
+ output wire fsmc_nwe, // 写入使能
90
+ output wire fsmc_noe, // 输出使能(读使能)
91
+ output wire fsmc_ne, // 片选信号
92
+ // 数据线(1'b1 -> 输入, 1'b0 -> 输出)
93
+ input wire[15:0] fsmc_data_i,
94
+ output wire[15:0] fsmc_data_o,
95
+ output wire[15:0] fsmc_data_t
96
+ );
97
+
98
+ /** AXI传输流程控制 **/
99
+ wire axi_trans_done; // AXI传输完成(指示)
100
+ reg axi_addr_available; // AXI地址寄存器可用(标志)
101
+ reg axi_wdata_available; // AXI写数据寄存器可用(标志)
102
+ reg axi_rd_reg; // 是否AXI读传输
103
+
104
+ // AXI地址寄存器可用(标志)
105
+ always @(posedge s_axi_clk or negedge s_axi_resetn)
106
+ begin
107
+ if(~s_axi_resetn)
108
+ axi_addr_available <= 1'b1;
109
+ else
110
+ # simulation_delay axi_addr_available <= axi_addr_available ?
111
+ (~((s_axi_arvalid & s_axi_arready) | (s_axi_awvalid & s_axi_awready))):axi_trans_done;
112
+ end
113
+
114
+ // AXI写数据寄存器可用(标志)
115
+ always @(posedge s_axi_clk or negedge s_axi_resetn)
116
+ begin
117
+ if(~s_axi_resetn)
118
+ axi_wdata_available <= 1'b1;
119
+ else
120
+ # simulation_delay axi_wdata_available <= axi_wdata_available ?
121
+ (~(s_axi_wvalid & s_axi_wready)):(axi_trans_done & (~axi_rd_reg));
122
+ end
123
+
124
+ /** AXI读/写地址通道 **/
125
+ wire axi_ar_req; // AXI读传输请求
126
+ wire axi_aw_req; // AXI写传输请求
127
+ wire arb_valid; // 仲裁结果有效
128
+ reg arb_valid_d; // 延迟1clk的仲裁结果有效
129
+ wire[1:0] arb_grant; // 仲裁授权(独热码)
130
+ reg[1:0] arb_grant_d; // 延迟1clk的仲裁授权(独热码)
131
+ reg[31:0] axi_addr_regs; // AXI地址寄存器
132
+
133
+ assign {s_axi_arready, s_axi_awready} = arb_grant_d;
134
+
135
+ assign axi_ar_req = s_axi_arvalid & axi_addr_available & (~arb_valid_d);
136
+ assign axi_aw_req = s_axi_awvalid & axi_addr_available & (~arb_valid_d);
137
+
138
+ // 延迟1clk的仲裁结果有效
139
+ always @(posedge s_axi_clk or negedge s_axi_resetn)
140
+ begin
141
+ if(~s_axi_resetn)
142
+ arb_valid_d <= 1'b0;
143
+ else
144
+ # simulation_delay arb_valid_d <= arb_valid;
145
+ end
146
+ // 延迟1clk的仲裁授权(独热码)
147
+ always @(posedge s_axi_clk or negedge s_axi_resetn)
148
+ begin
149
+ if(~s_axi_resetn)
150
+ arb_grant_d <= 2'b00;
151
+ else
152
+ # simulation_delay arb_grant_d <= arb_grant;
153
+ end
154
+
155
+ // AXI地址���存器
156
+ always @(posedge s_axi_clk)
157
+ begin
158
+ if((s_axi_arvalid & s_axi_arready) | (s_axi_awvalid & s_axi_awready))
159
+ // 断言: AR通道和AW通道不会同时握手!
160
+ # simulation_delay axi_addr_regs <= (s_axi_arvalid & s_axi_arready) ?
161
+ s_axi_araddr:s_axi_awaddr;
162
+ end
163
+ // 是否AXI读传输
164
+ always @(posedge s_axi_clk)
165
+ begin
166
+ if((s_axi_arvalid & s_axi_arready) | (s_axi_awvalid & s_axi_awready))
167
+ // 断言: AR通道和AW通道不会同时握手!
168
+ # simulation_delay axi_rd_reg <= s_axi_arvalid & s_axi_arready;
169
+ end
170
+
171
+ // Round-Robin仲裁器
172
+ round_robin_arbitrator #(
173
+ .chn_n(2),
174
+ .simulation_delay(simulation_delay)
175
+ )round_robin_arbitrator_u(
176
+ .clk(s_axi_clk),
177
+ .rst_n(s_axi_resetn),
178
+
179
+ .req({axi_ar_req, axi_aw_req}),
180
+ .grant(arb_grant),
181
+ .sel(),
182
+ .arb_valid(arb_valid)
183
+ );
184
+
185
+ /** AXI写响应通道 **/
186
+ reg axi_bvalid; // AXI写响应有效
187
+
188
+ assign s_axi_bresp = 2'b00;
189
+ assign s_axi_bvalid = axi_bvalid;
190
+
191
+ // AXI写响应有效
192
+ always @(posedge s_axi_clk or negedge s_axi_resetn)
193
+ begin
194
+ if(~s_axi_resetn)
195
+ axi_bvalid <= 1'b0;
196
+ else
197
+ # simulation_delay axi_bvalid <= axi_bvalid ? (~s_axi_bready):(axi_trans_done & (~axi_rd_reg));
198
+ end
199
+
200
+ /** AXI读数据通道 **/
201
+ // 配置寄存器片读数据输出
202
+ wire[15:0] cfg_reg_slice_rdata;
203
+ wire cfg_reg_slice_rdata_vld;
204
+ // FSMC控制器读数据输出
205
+ wire[15:0] m_axis_rd_data;
206
+ wire m_axis_rd_valid;
207
+ // AXI读数据
208
+ reg[31:0] axi_rdata_regs; // AXI读数据寄存器
209
+ reg axi_rdata_valid; // AXI读数据有效
210
+
211
+ assign s_axi_rdata = axi_rdata_regs;
212
+ assign s_axi_rresp = 2'b00;
213
+ assign s_axi_rvalid = axi_rdata_valid;
214
+
215
+ // AXI读数据寄存器
216
+ always @(posedge s_axi_clk)
217
+ begin
218
+ if((m_axis_rd_valid | cfg_reg_slice_rdata_vld) & axi_addr_regs[1])
219
+ # simulation_delay axi_rdata_regs[31:16] <= m_axis_rd_valid ? m_axis_rd_data:cfg_reg_slice_rdata;
220
+ end
221
+ always @(posedge s_axi_clk)
222
+ begin
223
+ if((m_axis_rd_valid | cfg_reg_slice_rdata_vld) & (~axi_addr_regs[1]))
224
+ # simulation_delay axi_rdata_regs[15:0] <= m_axis_rd_valid ? m_axis_rd_data:cfg_reg_slice_rdata;
225
+ end
226
+ // AXI读数据有效
227
+ always @(posedge s_axi_clk or negedge s_axi_resetn)
228
+ begin
229
+ if(~s_axi_resetn)
230
+ axi_rdata_valid <= 1'b0;
231
+ else
232
+ # simulation_delay axi_rdata_valid <= axi_rdata_valid ? (~s_axi_rready):(m_axis_rd_valid | cfg_reg_slice_rdata_vld);
233
+ end
234
+
235
+ /** AXI写数据通道 **/
236
+ reg[31:0] axi_wdata_regs; // AXI写数据寄存器
237
+ reg[3:0] axi_wstrb_regs; // AXI写字节使能寄存器
238
+
239
+ assign s_axi_wready = axi_wdata_available;
240
+
241
+ // AXI写数据寄存器
242
+ always @(posedge s_axi_clk)
243
+ begin
244
+ if(s_axi_wvalid & s_axi_wready)
245
+ # simulation_delay axi_wdata_regs <= s_axi_wdata;
246
+ end
247
+ // AXI写字节使能寄存器
248
+ always @(posedge s_axi_clk)
249
+ begin
250
+ if(s_axi_wvalid & s_axi_wready)
251
+ # simulation_delay axi_wstrb_regs <= s_axi_wstrb;
252
+ end
253
+
254
+ /**
255
+ 配置寄存器片
256
+
257
+ 偏移量0x000_0000~0x000_0FFF:
258
+ 子偏移量0x00 -> 7~0: 地址建立周期数 - 1
259
+ 15~8: 数据建立周期数 - 1
260
+ 子偏移量0x02 -> 7~0: 数据保持周期数 - 1
261
+ **/
262
+ reg[2:0] cfg_reg_slice_proc_ctrl; // 流程独热码
263
+ // FSMC控制器运行时参数
264
+ reg[7:0] addr_set; // 地址建立周期数 - 1
265
+ reg[7:0] data_set; // 数据建立周期数 - 1
266
+ reg[7:0] data_hold; // 数据保持周期数 - 1
267
+
268
+ assign cfg_reg_slice_rdata = 16'dx;
269
+ assign cfg_reg_slice_rdata_vld = cfg_reg_slice_proc_ctrl[2] & axi_rd_reg;
270
+
271
+ // 流程独热码
272
+ always @(posedge s_axi_clk or negedge s_axi_resetn)
273
+ begin
274
+ if(~s_axi_resetn)
275
+ cfg_reg_slice_proc_ctrl <= 3'b001;
276
+ else if((cfg_reg_slice_proc_ctrl[0] & (~axi_addr_available) &
277
+ (axi_addr_regs[25:12] == 14'd0) & (axi_rd_reg ? 1'b1:(~axi_wdata_available))) |
278
+ cfg_reg_slice_proc_ctrl[1] |
279
+ cfg_reg_slice_proc_ctrl[2])
280
+ # simulation_delay cfg_reg_slice_proc_ctrl <= {cfg_reg_slice_proc_ctrl[1:0], cfg_reg_slice_proc_ctrl[2]};
281
+ end
282
+
283
+ // 地址建立周期数 - 1
284
+ // 数据建立周期数 - 1
285
+ always @(posedge s_axi_clk)
286
+ begin
287
+ if(cfg_reg_slice_proc_ctrl[1] & (~axi_addr_regs[1]) & (~axi_rd_reg))
288
+ # simulation_delay {data_set, addr_set} <= axi_wdata_regs[15:0];
289
+ end
290
+ // 数据保持周期数 - 1
291
+ always @(posedge s_axi_clk)
292
+ begin
293
+ if(cfg_reg_slice_proc_ctrl[1] & axi_addr_regs[1] & (~axi_rd_reg))
294
+ # simulation_delay data_hold <= axi_wdata_regs[23:16];
295
+ end
296
+
297
+ /**
298
+ FSMC存储映射区
299
+
300
+ 偏移量0x000_1000~0x3FF_FFFF:
301
+ 子偏移量0x000_0000~0x3FF_EFFF -> FSMC
302
+ **/
303
+ reg[2:0] fsmc_proc_ctrl; // 流程独热码
304
+ // FSMC控制器流程控制
305
+ wire fsmc_ctrler_start;
306
+ wire fsmc_ctrler_done;
307
+
308
+ assign axi_trans_done = cfg_reg_slice_proc_ctrl[2] | fsmc_ctrler_done;
309
+
310
+ assign fsmc_ctrler_start = fsmc_proc_ctrl[1];
311
+
312
+ // 流程独热码
313
+ always @(posedge s_axi_clk or negedge s_axi_resetn)
314
+ begin
315
+ if(~s_axi_resetn)
316
+ fsmc_proc_ctrl <= 3'b001;
317
+ else if((fsmc_proc_ctrl[0] & (~axi_addr_available) &
318
+ (axi_addr_regs[25:12] != 14'd0) & (axi_rd_reg ? 1'b1:(~axi_wdata_available))) |
319
+ fsmc_proc_ctrl[1] |
320
+ (fsmc_proc_ctrl[2] & fsmc_ctrler_done))
321
+ # simulation_delay fsmc_proc_ctrl <= {fsmc_proc_ctrl[1:0], fsmc_proc_ctrl[2]};
322
+ end
323
+
324
+ /** FSMC控制器 **/
325
+ // 传输参数
326
+ wire[15:0] wdata; // 写数据
327
+ wire[1:0] data_mask; // 字节使能掩码
328
+ wire[25:0] trans_addr; // 传输地址
329
+ wire is_rd; // 是否读传输
330
+
331
+ assign wdata = axi_addr_regs[1] ? axi_wdata_regs[31:16]:axi_wdata_regs[15:0];
332
+ assign data_mask = axi_rd_reg ? 2'b00:(axi_addr_regs[1] ? (~axi_wstrb_regs[3:2]):(~axi_wstrb_regs[1:0]));
333
+ assign trans_addr = axi_addr_regs[25:0] - 32'h0000_1000;
334
+ assign is_rd = axi_rd_reg;
335
+
336
+ fsmc_ctrler #(
337
+ .simulation_delay(simulation_delay)
338
+ )fsmc_ctrler_u(
339
+ .clk(s_axi_clk),
340
+ .rst_n(s_axi_resetn),
341
+
342
+ .ctrler_start(fsmc_ctrler_start),
343
+ .ctrler_idle(),
344
+ .ctrler_done(fsmc_ctrler_done),
345
+
346
+ .addr_set(addr_set),
347
+ .data_set(data_set),
348
+ .data_hold(data_hold),
349
+
350
+ .wdata(wdata),
351
+ .data_mask(data_mask),
352
+ .trans_addr(trans_addr),
353
+ .is_rd(is_rd),
354
+
355
+ .m_axis_rd_data(m_axis_rd_data),
356
+ .m_axis_rd_valid(m_axis_rd_valid),
357
+
358
+ .fsmc_nbl(fsmc_nbl),
359
+ .fsmc_addr(fsmc_addr),
360
+ .fsmc_nwe(fsmc_nwe),
361
+ .fsmc_noe(fsmc_noe),
362
+ .fsmc_ne(fsmc_ne),
363
+ .fsmc_data_i(fsmc_data_i),
364
+ .fsmc_data_o(fsmc_data_o),
365
+ .fsmc_data_t(fsmc_data_t)
366
+ );
367
+
368
+ endmodule
CutestPanda_Opensoc/103_generic_fsmc_ctrler/rtl/fsmc_ctrler.v ADDED
@@ -0,0 +1,210 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ MIT License
3
+
4
+ Copyright (c) 2024 Panda, 2257691535@qq.com
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
23
+ */
24
+
25
+ `timescale 1ns / 1ps
26
+ /********************************************************************
27
+ 本模块: FSMC控制器
28
+
29
+ 描述:
30
+ 通用FSMC控制器
31
+
32
+ 每次FSMC传输: 地址建立 -> 数据建立 -> 数据保持
33
+
34
+ 注意:
35
+
36
+
37
+ 协议:
38
+ AP CTRL
39
+ AXIS MASTER
40
+ FSMC MASTER
41
+
42
+ 作者: 陈家耀
43
+ 日期: 2024/07/18
44
+ ********************************************************************/
45
+
46
+
47
+ module fsmc_ctrler #(
48
+ parameter real simulation_delay = 0 // 仿真延时
49
+ )(
50
+ // 时钟和复位
51
+ input wire clk,
52
+ input wire rst_n,
53
+
54
+ // 流程控制
55
+ input wire ctrler_start,
56
+ output wire ctrler_idle,
57
+ output wire ctrler_done,
58
+
59
+ // 运行时参数
60
+ // 断言:运行时参数在控制器开始工作后保持有效值不变!
61
+ input wire[7:0] addr_set, // 地址建立周期数 - 1
62
+ input wire[7:0] data_set, // 数据建立周期数 - 1
63
+ input wire[7:0] data_hold, // 数据保持周期数 - 1
64
+
65
+ // 传输参数
66
+ input wire[15:0] wdata, // 写数据
67
+ input wire[1:0] data_mask, // 字节使能掩码
68
+ input wire[25:0] trans_addr, // 传输地址
69
+ input wire is_rd, // 是否读传输
70
+
71
+ // 读数据输出
72
+ output wire[15:0] m_axis_rd_data,
73
+ output wire m_axis_rd_valid,
74
+
75
+ // FSMC MASTER
76
+ output wire[1:0] fsmc_nbl, // 数据掩码
77
+ output wire[25:0] fsmc_addr, // 地址线
78
+ output wire fsmc_nwe, // 写入使能
79
+ output wire fsmc_noe, // 输出使能(读使能)
80
+ output wire fsmc_ne, // 片选信号
81
+ // 数据线(1'b1 -> 输入, 1'b0 -> 输出)
82
+ input wire[15:0] fsmc_data_i,
83
+ output wire[15:0] fsmc_data_o,
84
+ output wire[15:0] fsmc_data_t
85
+ );
86
+
87
+ /** 锁存的传输信息 **/
88
+ reg[15:0] wdata_latched; // 锁存的写数据
89
+ reg[1:0] data_mask_latched; // 锁存的字节使能掩码
90
+ reg[25:0] trans_addr_latched; // 锁存的传输地址
91
+ reg is_rd_latched; // 锁存的是否读传输
92
+
93
+ // 锁存的写数据
94
+ always @(posedge clk)
95
+ begin
96
+ if(ctrler_start & ctrler_idle)
97
+ # simulation_delay wdata_latched <= wdata;
98
+ end
99
+ // 锁存的字节使能掩码
100
+ always @(posedge clk)
101
+ begin
102
+ if(ctrler_start & ctrler_idle)
103
+ # simulation_delay data_mask_latched <= data_mask;
104
+ end
105
+ // 锁存的传输地址
106
+ always @(posedge clk)
107
+ begin
108
+ if(ctrler_start & ctrler_idle)
109
+ # simulation_delay trans_addr_latched <= trans_addr;
110
+ end
111
+ // 锁存的是否读传输
112
+ always @(posedge clk)
113
+ begin
114
+ if(ctrler_start & ctrler_idle)
115
+ # simulation_delay is_rd_latched <= is_rd;
116
+ end
117
+
118
+ /**
119
+ 流程控制
120
+
121
+ 空闲 -> 地址建立 -> 数据建立 -> 数据保持
122
+ **/
123
+ reg[3:0] proc_onehot; // 流程独热码
124
+ reg[7:0] setup_cnt; // 地址/数据建立计数器
125
+ reg[7:0] hold_cnt; // 数据保持计数器
126
+ wire addr_set_done; // 地址建立完成指示
127
+ wire data_set_done; // 数据建立完成指示
128
+ wire data_hold_done; // 数据保持完成指示
129
+
130
+ assign ctrler_idle = proc_onehot[0];
131
+ assign ctrler_done = data_hold_done;
132
+
133
+ assign addr_set_done = proc_onehot[1] & (setup_cnt == addr_set);
134
+ assign data_set_done = proc_onehot[2] & (setup_cnt == data_set);
135
+ assign data_hold_done = proc_onehot[3] & (hold_cnt == data_hold);
136
+
137
+ // 流程独热码
138
+ always @(posedge clk or negedge rst_n)
139
+ begin
140
+ if(~rst_n)
141
+ proc_onehot <= 4'b0001;
142
+ else if((proc_onehot[0] & ctrler_start) |
143
+ (proc_onehot[1] & (setup_cnt == addr_set)) |
144
+ (proc_onehot[2] & (setup_cnt == data_set)) |
145
+ (proc_onehot[3] & (hold_cnt == data_hold)))
146
+ # simulation_delay proc_onehot <= {proc_onehot[2:0], proc_onehot[3]};
147
+ end
148
+
149
+ // 地址/数据建立计数器
150
+ always @(posedge clk or negedge rst_n)
151
+ begin
152
+ if(~rst_n)
153
+ setup_cnt <= 8'd0;
154
+ else if(proc_onehot[1] | proc_onehot[2])
155
+ # simulation_delay setup_cnt <= (setup_cnt == (proc_onehot[1] ? addr_set:data_set)) ?
156
+ 8'd0:(setup_cnt + 8'd1);
157
+ end
158
+
159
+ // 数据保持计数器
160
+ always @(posedge clk)
161
+ begin
162
+ if(proc_onehot[3])
163
+ # simulation_delay hold_cnt <= hold_cnt + 8'd1;
164
+ else
165
+ # simulation_delay hold_cnt <= 8'd0;
166
+ end
167
+
168
+ /** FSMC主接口 **/
169
+ reg fsmc_nwe_reg; // 写入使能
170
+ reg fsmc_noe_reg; // 输出使能(读使能)
171
+ reg fsmc_data_t_reg; // 数据三态方向选择
172
+
173
+ assign fsmc_nbl = data_mask_latched;
174
+ assign fsmc_addr = trans_addr_latched;
175
+ assign fsmc_nwe = fsmc_nwe_reg;
176
+ assign fsmc_noe = fsmc_noe_reg;
177
+ assign fsmc_ne = ctrler_idle;
178
+ assign fsmc_data_o = wdata_latched;
179
+ assign fsmc_data_t = {16{fsmc_data_t_reg}};
180
+
181
+ // 写入使能
182
+ always @(posedge clk or negedge rst_n)
183
+ begin
184
+ if(~rst_n)
185
+ fsmc_nwe_reg <= 1'b1;
186
+ else if((~is_rd_latched) & (addr_set_done | data_set_done))
187
+ # simulation_delay fsmc_nwe_reg <= data_set_done;
188
+ end
189
+ // 输出使能(读使能)
190
+ always @(posedge clk or negedge rst_n)
191
+ begin
192
+ if(~rst_n)
193
+ fsmc_noe_reg <= 1'b1;
194
+ else if(is_rd_latched & (addr_set_done | data_hold_done))
195
+ # simulation_delay fsmc_noe_reg <= data_hold_done;
196
+ end
197
+ // 数据三态方向选择
198
+ always @(posedge clk or negedge rst_n)
199
+ begin
200
+ if(~rst_n)
201
+ fsmc_data_t_reg <= 1'b1;
202
+ else if(((~is_rd_latched) & addr_set_done) | data_hold_done)
203
+ # simulation_delay fsmc_data_t_reg <= data_hold_done;
204
+ end
205
+
206
+ /** 读数据输出 **/
207
+ assign m_axis_rd_data = fsmc_data_i;
208
+ assign m_axis_rd_valid = is_rd_latched & proc_onehot[3] & (hold_cnt == 8'd0);
209
+
210
+ endmodule
CutestPanda_Opensoc/103_generic_fsmc_ctrler/rtl/round_robin_arbitrator.v ADDED
@@ -0,0 +1,105 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ MIT License
3
+
4
+ Copyright (c) 2024 Panda, 2257691535@qq.com
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
23
+ */
24
+
25
+ `timescale 1ns / 1ps
26
+ /********************************************************************
27
+ 本模块: Round-Robin仲裁器
28
+
29
+ 描述:
30
+ 基于Round-Robin算法的0时延仲裁器
31
+
32
+ 注意:
33
+
34
+
35
+ 协议:
36
+ ARB SLAVE
37
+
38
+ 作者: 陈家耀
39
+ 日期: 2024/04/28
40
+ ********************************************************************/
41
+
42
+
43
+ module round_robin_arbitrator #(
44
+ parameter integer chn_n = 4, // 通道个数(必须>=2)
45
+ parameter real simulation_delay = 1 // 仿真延时
46
+ )(
47
+ // 时钟和复位
48
+ input wire clk,
49
+ input wire rst_n,
50
+
51
+ // 仲裁器
52
+ input wire[chn_n-1:0] req, // 请求
53
+ output wire[chn_n-1:0] grant, // 授权(独热码)
54
+ output wire[clogb2(chn_n-1):0] sel, // 选择(相当于授权的二进制表示)
55
+ output wire arb_valid // 仲裁结果有效
56
+ );
57
+
58
+ // 计算log2(bit_depth)
59
+ function integer clogb2 (input integer bit_depth);
60
+ integer temp;
61
+ begin
62
+ temp = bit_depth;
63
+ for(clogb2 = -1;temp > 0;clogb2 = clogb2 + 1)
64
+ temp = temp >> 1;
65
+ end
66
+ endfunction
67
+ // 独热码 -> 二进制码
68
+ function [clogb2(chn_n-1):0] onehot_to_bin(input[chn_n-1:0] onehot);
69
+ integer i;
70
+ begin
71
+ onehot_to_bin = 0;
72
+
73
+ for(i = 0;i < chn_n;i = i + 1)
74
+ begin
75
+ if(onehot[i])
76
+ onehot_to_bin = i;
77
+ end
78
+ end
79
+ endfunction
80
+
81
+ /** 仲裁优先级 **/
82
+ reg[chn_n-1:0] priority_cnt; // 优先级独热码计数器
83
+
84
+ // 优先级独热码计数器
85
+ // 1的位置表征最高优先级的通道
86
+ always @(posedge clk or negedge rst_n)
87
+ begin
88
+ if(~rst_n)
89
+ priority_cnt <= {{(chn_n-1){1'b0}}, 1'b1};
90
+ else if(|req) // 当前有请求
91
+ # simulation_delay priority_cnt <= {grant[chn_n-2:0], grant[chn_n-1]};
92
+ end
93
+
94
+ /** 仲裁授权与选择 **/
95
+ wire[chn_n*2-1:0] double_req;
96
+ wire[chn_n*2-1:0] double_grant;
97
+
98
+ assign double_req = {req, req};
99
+ assign double_grant = double_req & (~(double_req - priority_cnt));
100
+
101
+ assign grant = double_grant[chn_n-1:0] | double_grant[chn_n*2-1:chn_n];
102
+ assign sel = onehot_to_bin(grant);
103
+ assign arb_valid = |req;
104
+
105
+ endmodule
CutestPanda_Opensoc/103_generic_fsmc_ctrler/tb/axi_fsmc/agents.sv ADDED
@@ -0,0 +1,413 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ MIT License
3
+
4
+ Copyright (c) 2024 Panda, 2257691535@qq.com
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
23
+ */
24
+
25
+ `timescale 1ns / 1ps
26
+
27
+ `ifndef __AGENT_H
28
+
29
+ `define __AGENT_H
30
+
31
+ // `define BlkCtrlMstAgt
32
+ `define AXIMstAgt
33
+ // `define AXISlvAgt
34
+ // `define AXISMstAgt
35
+ // `define AXISSlvAgt
36
+ // `define APBSlvAgt
37
+ // `define AHBMstAgt
38
+
39
+ `include "transactions.sv"
40
+ `include "sequencers.sv"
41
+ `include "drivers.sv"
42
+ `include "monitors.sv"
43
+
44
+ /** 代理:块级控制主机 **/
45
+ `ifdef BlkCtrlMstAgt
46
+ class BlkCtrlMasterAgent #(
47
+ real out_drive_t = 1 // 输出驱动延迟量
48
+ )extends uvm_agent;
49
+
50
+ // 组件
51
+ local BlkCtrlSeqr sequencer; // 序列发生器
52
+ local BlkCtrlMasterDriver #(.out_drive_t(out_drive_t)) driver; // 驱动器
53
+
54
+ // 注册component
55
+ `uvm_component_param_utils(BlkCtrlMasterAgent #(.out_drive_t(out_drive_t)))
56
+
57
+ function new(string name = "BlkCtrlMasterAgent", uvm_component parent = null);
58
+ super.new(name, parent);
59
+ endfunction
60
+
61
+ virtual function void build_phase(uvm_phase phase);
62
+ super.build_phase(phase);
63
+
64
+ this.sequencer = BlkCtrlSeqr::type_id::create("sqr", this); // 创建sequencer
65
+ this.driver = BlkCtrlMasterDriver #(.out_drive_t(out_drive_t))::type_id::create("drv", this); // 创建driver
66
+
67
+ `uvm_info("BlkCtrlMasterAgent", "BlkCtrlMasterAgent built!", UVM_LOW)
68
+ endfunction
69
+
70
+ virtual function void connect_phase(uvm_phase phase);
71
+ super.connect_phase(phase);
72
+
73
+ this.driver.seq_item_port.connect(this.sequencer.seq_item_export); // 连接sequence-port
74
+ endfunction
75
+
76
+ endclass
77
+ `endif
78
+
79
+ /** 代理:AXI主机 **/
80
+ `ifdef AXIMstAgt
81
+ class AXIMasterAgent #(
82
+ real out_drive_t = 1, // 输出驱动延迟量
83
+ integer addr_width = 32, // 地址位宽(1~64)
84
+ integer data_width = 32, // 数据位宽(8 | 16 | 32 | 64 | 128 | 256 | 512 | 1024)
85
+ integer bresp_width = 2, // 写响应信号位宽(0 | 2 | 3)
86
+ integer rresp_width = 2 // 读响应信号位宽(0 | 2 | 3)
87
+ )extends uvm_agent;
88
+
89
+ // 组件
90
+ local AXISequencer #(.addr_width(addr_width), .data_width(data_width),
91
+ .bresp_width(bresp_width), .rresp_width(rresp_width)) sequencer; // 序列发生器
92
+ local AXIMasterDriver #(.out_drive_t(out_drive_t), .addr_width(addr_width), .data_width(data_width),
93
+ .bresp_width(bresp_width), .rresp_width(rresp_width)) driver; // 驱动器
94
+ local AXIMonitor #(.out_drive_t(out_drive_t), .addr_width(addr_width), .data_width(data_width),
95
+ .bresp_width(bresp_width), .rresp_width(rresp_width)) monitor; // 监测器
96
+
97
+ // 通信端口
98
+ uvm_analysis_port #(AXITrans #(.addr_width(addr_width), .data_width(data_width),
99
+ .bresp_width(bresp_width), .rresp_width(rresp_width))) rd_trans_analysis_port;
100
+ uvm_analysis_port #(AXITrans #(.addr_width(addr_width), .data_width(data_width),
101
+ .bresp_width(bresp_width), .rresp_width(rresp_width))) wt_trans_analysis_port;
102
+
103
+ // 注册component
104
+ `uvm_component_param_utils(AXIMasterAgent #(.out_drive_t(out_drive_t), .addr_width(addr_width), .data_width(data_width), .bresp_width(bresp_width), .rresp_width(rresp_width)))
105
+
106
+ function new(string name = "AXIMasterAgent", uvm_component parent = null);
107
+ super.new(name, parent);
108
+ endfunction
109
+
110
+ virtual function void build_phase(uvm_phase phase);
111
+ super.build_phase(phase);
112
+
113
+ if (this.is_active == UVM_ACTIVE)
114
+ begin
115
+ this.sequencer = AXISequencer #(.addr_width(addr_width), .data_width(data_width),
116
+ .bresp_width(bresp_width), .rresp_width(rresp_width))::type_id::create("sqr", this); // 创建sequencer
117
+ this.driver = AXIMasterDriver #(.out_drive_t(out_drive_t), .addr_width(addr_width), .data_width(data_width),
118
+ .bresp_width(bresp_width), .rresp_width(rresp_width))::type_id::create("drv", this); // 创建driver
119
+ end
120
+
121
+ this.monitor = AXIMonitor #(.out_drive_t(out_drive_t), .addr_width(addr_width), .data_width(data_width),
122
+ .bresp_width(bresp_width), .rresp_width(rresp_width))::type_id::create("mon", this); // 创建monitor
123
+
124
+ `uvm_info("AXIMasterAgent", "AXIMasterAgent built!", UVM_LOW)
125
+ endfunction
126
+
127
+ virtual function void connect_phase(uvm_phase phase);
128
+ super.connect_phase(phase);
129
+
130
+ if(this.is_active == UVM_ACTIVE)
131
+ this.driver.seq_item_port.connect(this.sequencer.seq_item_export); // 连接sequence-port
132
+
133
+ this.rd_trans_analysis_port = this.monitor.rd_trans_analysis_port;
134
+ this.wt_trans_analysis_port = this.monitor.wt_trans_analysis_port;
135
+ endfunction
136
+
137
+ endclass
138
+ `endif
139
+
140
+ /** 代理:AXI从机 **/
141
+ `ifdef AXISlvAgt
142
+ class AXISlaveAgent #(
143
+ real out_drive_t = 1, // 输出驱动延迟量
144
+ integer addr_width = 32, // 地址位宽(1~64)
145
+ integer data_width = 32, // 数据位宽(8 | 16 | 32 | 64 | 128 | 256 | 512 | 1024)
146
+ integer bresp_width = 2, // 写响应信号位宽(0 | 2 | 3)
147
+ integer rresp_width = 2 // 读响应信号位宽(0 | 2 | 3)
148
+ )extends uvm_agent;
149
+
150
+ // 组件
151
+ local AXISequencer #(.addr_width(addr_width), .data_width(data_width),
152
+ .bresp_width(bresp_width), .rresp_width(rresp_width)) sequencer; // 序列发生器
153
+ local AXISlaveDriver #(.out_drive_t(out_drive_t), .addr_width(addr_width), .data_width(data_width),
154
+ .bresp_width(bresp_width), .rresp_width(rresp_width)) driver; // 驱动器
155
+ local AXIMonitor #(.out_drive_t(out_drive_t), .addr_width(addr_width), .data_width(data_width),
156
+ .bresp_width(bresp_width), .rresp_width(rresp_width)) monitor; // 监测器
157
+
158
+ // 通信端口
159
+ uvm_analysis_port #(AXITrans #(.addr_width(addr_width), .data_width(data_width),
160
+ .bresp_width(bresp_width), .rresp_width(rresp_width))) rd_trans_analysis_port;
161
+ uvm_analysis_port #(AXITrans #(.addr_width(addr_width), .data_width(data_width),
162
+ .bresp_width(bresp_width), .rresp_width(rresp_width))) wt_trans_analysis_port;
163
+
164
+ // 注册component
165
+ `uvm_component_param_utils(AXISlaveAgent #(.out_drive_t(out_drive_t), .addr_width(addr_width), .data_width(data_width), .bresp_width(bresp_width), .rresp_width(rresp_width)))
166
+
167
+ function new(string name = "AXISlaveAgent", uvm_component parent = null);
168
+ super.new(name, parent);
169
+ endfunction
170
+
171
+ virtual function void build_phase(uvm_phase phase);
172
+ super.build_phase(phase);
173
+
174
+ if (this.is_active == UVM_ACTIVE)
175
+ begin
176
+ this.sequencer = AXISequencer #(.addr_width(addr_width), .data_width(data_width),
177
+ .bresp_width(bresp_width), .rresp_width(rresp_width))::type_id::create("sqr", this); // 创建sequencer
178
+ this.driver = AXISlaveDriver #(.out_drive_t(out_drive_t), .addr_width(addr_width), .data_width(data_width),
179
+ .bresp_width(bresp_width), .rresp_width(rresp_width))::type_id::create("drv", this); // 创建driver
180
+ end
181
+
182
+ this.monitor = AXIMonitor #(.out_drive_t(out_drive_t), .addr_width(addr_width), .data_width(data_width),
183
+ .bresp_width(bresp_width), .rresp_width(rresp_width))::type_id::create("mon", this); // 创建monitor
184
+
185
+ `uvm_info("AXISlaveAgent", "AXISlaveAgent built!", UVM_LOW)
186
+ endfunction
187
+
188
+ virtual function void connect_phase(uvm_phase phase);
189
+ super.connect_phase(phase);
190
+
191
+ if(this.is_active == UVM_ACTIVE)
192
+ this.driver.seq_item_port.connect(this.sequencer.seq_item_export); // 连接sequence-port
193
+
194
+ this.rd_trans_analysis_port = this.monitor.rd_trans_analysis_port;
195
+ this.wt_trans_analysis_port = this.monitor.wt_trans_analysis_port;
196
+ endfunction
197
+
198
+ endclass
199
+ `endif
200
+
201
+ /** 代理:AXIS主机 **/
202
+ `ifdef AXISMstAgt
203
+ class AXISMasterAgent #(
204
+ real out_drive_t = 1, // 输出驱动延迟量
205
+ integer data_width = 32, // 数据位宽(必须能被8整除)
206
+ integer user_width = 0 // 用户数据位宽
207
+ )extends uvm_agent;
208
+
209
+ // 组件
210
+ local AXISSequencer #(.data_width(data_width), .user_width(user_width)) sequencer; // 序列发生器
211
+ local AXISMasterDriver #(.out_drive_t(out_drive_t), .data_width(data_width), .user_width(user_width)) driver; // 驱动器
212
+ local AXISMonitor #(.out_drive_t(out_drive_t), .data_width(data_width), .user_width(user_width)) monitor; // 监测器
213
+
214
+ // 通信端口
215
+ uvm_analysis_port #(AXISTrans #(.data_width(data_width), .user_width(user_width))) axis_analysis_port;
216
+
217
+ // 注册component
218
+ `uvm_component_param_utils(AXISMasterAgent #(.out_drive_t(out_drive_t), .data_width(data_width), .user_width(user_width)))
219
+
220
+ function new(string name = "AXISMasterAgent", uvm_component parent = null);
221
+ super.new(name, parent);
222
+ endfunction
223
+
224
+ virtual function void build_phase(uvm_phase phase);
225
+ super.build_phase(phase);
226
+
227
+ if (this.is_active == UVM_ACTIVE)
228
+ begin
229
+ this.sequencer = AXISSequencer #(.data_width(data_width), .user_width(user_width))::
230
+ type_id::create("sqr", this); // 创建sequencer
231
+ this.driver = AXISMasterDriver #(.out_drive_t(out_drive_t), .data_width(data_width), .user_width(user_width))::
232
+ type_id::create("drv", this); // 创建driver
233
+ end
234
+
235
+ this.monitor = AXISMonitor #(.out_drive_t(out_drive_t), .data_width(data_width), .user_width(user_width))::
236
+ type_id::create("mon", this); // 创建monitor
237
+
238
+ `uvm_info("AXISMasterAgent", "AXISMasterAgent built!", UVM_LOW)
239
+ endfunction
240
+
241
+ virtual function void connect_phase(uvm_phase phase);
242
+ super.connect_phase(phase);
243
+
244
+ if(this.is_active == UVM_ACTIVE)
245
+ this.driver.seq_item_port.connect(this.sequencer.seq_item_export); // 连接sequence-port
246
+
247
+ this.axis_analysis_port = this.monitor.in_analysis_port;
248
+ endfunction
249
+
250
+ endclass
251
+ `endif
252
+
253
+ /** 代理:AXIS从机 **/
254
+ `ifdef AXISSlvAgt
255
+ class AXISSlaveAgent #(
256
+ real out_drive_t = 1, // 输出驱动延迟量
257
+ integer data_width = 32, // 数据位宽(必须能被8整除)
258
+ integer user_width = 0 // 用户数据位宽
259
+ )extends uvm_agent;
260
+
261
+ // 组件
262
+ local AXISSlaveDriver #(.out_drive_t(out_drive_t), .data_width(data_width), .user_width(user_width)) driver; // 驱动器
263
+ local AXISMonitor #(.out_drive_t(out_drive_t), .data_width(data_width), .user_width(user_width)) monitor; // 监测器
264
+
265
+ // 通信端口
266
+ uvm_analysis_port #(AXISTrans #(.data_width(data_width), .user_width(user_width))) axis_analysis_port;
267
+
268
+ // 注册component
269
+ `uvm_component_param_utils(AXISSlaveAgent #(.out_drive_t(out_drive_t), .data_width(data_width), .user_width(user_width)))
270
+
271
+ function new(string name = "AXISSlaveAgent", uvm_component parent = null);
272
+ super.new(name, parent);
273
+ endfunction
274
+
275
+ virtual function void build_phase(uvm_phase phase);
276
+ super.build_phase(phase);
277
+
278
+ if (this.is_active == UVM_ACTIVE)
279
+ begin
280
+ this.driver = AXISSlaveDriver #(.out_drive_t(out_drive_t), .data_width(data_width), .user_width(user_width))::
281
+ type_id::create("drv", this); // 创建driver
282
+ end
283
+
284
+ this.monitor = AXISMonitor #(.out_drive_t(out_drive_t), .data_width(data_width), .user_width(user_width))::
285
+ type_id::create("mon", this); // 创建monitor
286
+
287
+ `uvm_info("AXISSlaveAgent", "AXISSlaveAgent built!", UVM_LOW)
288
+ endfunction
289
+
290
+ virtual function void connect_phase(uvm_phase phase);
291
+ super.connect_phase(phase);
292
+
293
+ this.axis_analysis_port = this.monitor.in_analysis_port;
294
+ endfunction
295
+
296
+ endclass
297
+ `endif
298
+
299
+ /** 代理:APB从机 **/
300
+ `ifdef APBSlvAgt
301
+ class APBSlaveAgent #(
302
+ real out_drive_t = 1, // 输出驱动延迟量
303
+ integer addr_width = 32, // 地址位宽(1~32)
304
+ integer data_width = 32 // 数据位宽(8 | 16 | 32)
305
+ )extends uvm_agent;
306
+
307
+ // 组件
308
+ local APBSequencer #(.addr_width(addr_width), .data_width(data_width)) sequencer; // 序列发生器
309
+ local APBSlaveDriver #(.out_drive_t(out_drive_t), .addr_width(addr_width), .data_width(data_width)) driver; // 驱动器
310
+ local APBMonitor #(.out_drive_t(out_drive_t), .addr_width(addr_width), .data_width(data_width)) monitor; // 监测器
311
+
312
+ // 通信端口
313
+ uvm_analysis_port #(APBTrans #(.addr_width(addr_width), .data_width(data_width))) apb_analysis_port;
314
+
315
+ // 注册component
316
+ `uvm_component_param_utils(APBSlaveAgent #(.out_drive_t(out_drive_t), .addr_width(addr_width), .data_width(data_width)))
317
+
318
+ function new(string name = "APBSlaveAgent", uvm_component parent = null);
319
+ super.new(name, parent);
320
+ endfunction
321
+
322
+ virtual function void build_phase(uvm_phase phase);
323
+ super.build_phase(phase);
324
+
325
+ if (this.is_active == UVM_ACTIVE)
326
+ begin
327
+ this.sequencer = APBSequencer #(.addr_width(addr_width), .data_width(data_width))::
328
+ type_id::create("sqr", this); // 创建sequencer
329
+ this.driver = APBSlaveDriver #(.out_drive_t(out_drive_t), .addr_width(addr_width), .data_width(data_width))::
330
+ type_id::create("drv", this); // 创建driver
331
+ end
332
+
333
+ this.monitor = APBMonitor #(.out_drive_t(out_drive_t), .addr_width(addr_width), .data_width(data_width))::
334
+ type_id::create("mon", this); // 创建monitor
335
+
336
+ `uvm_info("APBSlaveAgent", "APBSlaveAgent built!", UVM_LOW)
337
+ endfunction
338
+
339
+ virtual function void connect_phase(uvm_phase phase);
340
+ super.connect_phase(phase);
341
+
342
+ if(this.is_active == UVM_ACTIVE)
343
+ this.driver.seq_item_port.connect(this.sequencer.seq_item_export); // 连接sequence-port
344
+
345
+ this.apb_analysis_port = this.monitor.in_analysis_port;
346
+ endfunction
347
+
348
+ endclass
349
+ `endif
350
+
351
+ /** 代理:AHB主机 **/
352
+ `ifdef AHBMstAgt
353
+ class AHBMasterAgent #(
354
+ real out_drive_t = 1, // 输出驱动延迟量
355
+ integer slave_n = 1, // 从机个数
356
+ integer addr_width = 32, // 地址位宽(10~64)
357
+ integer data_width = 32, // 数据位宽(8 | 16 | 32 | 64 | 128 | 256 | 512 | 1024)
358
+ integer burst_width = 3, // 突发类型位宽(0~3)
359
+ integer prot_width = 4, // 保护类型位宽(0 | 4 | 7)
360
+ integer master_width = 1 // 主机标识位宽(0~8)
361
+ )extends uvm_agent;
362
+
363
+ // 组件
364
+ local AHBSequencer #(.addr_width(addr_width), .data_width(data_width), .burst_width(burst_width),
365
+ .prot_width(prot_width), .master_width(master_width)) sequencer; // 序列发生器
366
+ local AHBMasterDriver #(.out_drive_t(out_drive_t), .slave_n(slave_n), .addr_width(addr_width), .data_width(data_width),
367
+ .burst_width(burst_width), .prot_width(prot_width), .master_width(master_width)) driver; // 驱动器
368
+ local AHBMonitor #(.out_drive_t(out_drive_t), .slave_n(slave_n), .addr_width(addr_width), .data_width(data_width),
369
+ .burst_width(burst_width), .prot_width(prot_width), .master_width(master_width)) monitor; // 监测器
370
+
371
+ // 通信端口
372
+ uvm_analysis_port #(AHBTrans #(.addr_width(addr_width), .data_width(data_width),
373
+ .burst_width(burst_width), .prot_width(prot_width), .master_width(master_width))) ahb_analysis_port;
374
+
375
+ // 注册component
376
+ `uvm_component_param_utils(AHBMasterAgent #(.out_drive_t(out_drive_t), .slave_n(slave_n), .addr_width(addr_width), .data_width(data_width), .burst_width(burst_width), .prot_width(prot_width), .master_width(master_width)))
377
+
378
+ function new(string name = "AHBMasterAgent", uvm_component parent = null);
379
+ super.new(name, parent);
380
+ endfunction
381
+
382
+ virtual function void build_phase(uvm_phase phase);
383
+ super.build_phase(phase);
384
+
385
+ if (this.is_active == UVM_ACTIVE)
386
+ begin
387
+ this.sequencer = AHBSequencer #(.addr_width(addr_width), .data_width(data_width), .burst_width(burst_width),
388
+ .prot_width(prot_width), .master_width(master_width))::type_id::create("sqr", this); // 创建sequencer
389
+ this.driver = AHBMasterDriver #(.out_drive_t(out_drive_t), .slave_n(slave_n), .addr_width(addr_width),
390
+ .data_width(data_width), .burst_width(burst_width), .prot_width(prot_width),
391
+ .master_width(master_width))::type_id::create("drv", this); // 创建driver
392
+ end
393
+
394
+ this.monitor = AHBMonitor #(.out_drive_t(out_drive_t), .slave_n(slave_n), .addr_width(addr_width),
395
+ .data_width(data_width), .burst_width(burst_width), .prot_width(prot_width), .master_width(master_width))::
396
+ type_id::create("mon", this); // 创建monitor
397
+
398
+ `uvm_info("AHBMasterAgent", "AHBMasterAgent built!", UVM_LOW)
399
+ endfunction
400
+
401
+ virtual function void connect_phase(uvm_phase phase);
402
+ super.connect_phase(phase);
403
+
404
+ if(this.is_active == UVM_ACTIVE)
405
+ this.driver.seq_item_port.connect(this.sequencer.seq_item_export); // 连接sequence-port
406
+
407
+ this.ahb_analysis_port = this.monitor.in_analysis_port;
408
+ endfunction
409
+
410
+ endclass
411
+ `endif
412
+
413
+ `endif