OSDN Git Service

more tests for adc/sbc.
[motonesfpga/motonesfpga.git] / de1_nes / motonesfpga_common.vhd
1 --  
2 --   MOTO NES FPGA Common Routines
3 --  
4
5 -------------------------------------------------------------
6 -------------------------------------------------------------
7 -------------------- package declaration --------------------
8 -------------------------------------------------------------
9 -------------------------------------------------------------
10
11 library ieee;
12 use ieee.std_logic_1164.all;
13
14 package motonesfpga_common is
15
16 procedure d_print(msg : string);
17
18 function conv_hex8(ival : integer) return string;
19
20 function conv_hex8(ival : std_logic_vector) return string;
21
22 function conv_hex16(ival : integer) return string;
23
24 function conv_hex16(ival : std_logic_vector) return string;
25
26 end motonesfpga_common;
27
28
29 -------------------------------------------------------------
30 -------------------------------------------------------------
31 ----------------------- package body ------------------------
32 -------------------------------------------------------------
33 -------------------------------------------------------------
34
35 package body motonesfpga_common is
36
37 use ieee.std_logic_unsigned.conv_integer;
38
39 procedure d_print(msg : string) is
40 --use std.textio.all;
41 --use ieee.std_logic_textio.all;
42 --variable out_l : line;
43 begin
44 --    write(out_l, msg);
45 --    writeline(output, out_l);
46 end  procedure;
47
48 ---ival : 0x0000 - 0xffff
49 function conv_hex8(ival : integer) return string is
50 variable tmp1, tmp2 : integer;
51 variable hex_chr: string (1 to 16) := "0123456789abcdef";
52 begin
53     tmp2 := (ival mod 16 ** 2) / 16 ** 1;
54     tmp1 := ival mod 16 ** 1;
55     return hex_chr(tmp2 + 1) & hex_chr(tmp1 + 1);
56 end;
57
58 function conv_hex8(ival : std_logic_vector) return string is
59 begin
60     return conv_hex8(conv_integer(ival));
61 end;
62
63 function conv_hex16(ival : integer) return string is
64 variable tmp1, tmp2 : integer;
65 variable hex_chr: string (1 to 16) := "0123456789abcdef";
66 begin
67     tmp2 := ival / 256;
68     tmp1 := ival mod 256;
69     return conv_hex8(tmp2) & conv_hex8(tmp1);
70 end;
71
72 function conv_hex16(ival : std_logic_vector) return string is
73 begin
74     return conv_hex16(conv_integer(ival));
75 end;
76
77 end motonesfpga_common;
78
79 -------------------------------------------------------------
80 -------------------------------------------------------------
81 -------------------------------------------------------------
82 ------------------ other common modules ---------------------
83 -------------------------------------------------------------
84 -------------------------------------------------------------
85 -------------------------------------------------------------
86
87 ----------------------------------------
88 --- d-flipflop with set/reset
89 ----------------------------------------
90
91 library ieee;
92 use ieee.std_logic_1164.all;
93
94 entity d_flip_flop is 
95     generic (
96             dsize : integer := 8
97             );
98     port (  
99             clk     : in std_logic;
100             res_n   : in std_logic;
101             set_n   : in std_logic;
102             we_n    : in std_logic;
103             d       : in std_logic_vector (dsize - 1 downto 0);
104             q       : out std_logic_vector (dsize - 1 downto 0)
105         );
106 end d_flip_flop;
107
108 architecture rtl of d_flip_flop is
109 begin
110
111     process (clk, res_n, set_n, d)
112     begin
113         if (res_n = '0') then
114             q <= (others => '0');
115         elsif (set_n = '0') then
116             q <= d;
117         elsif (rising_edge(clk)) then
118             if (we_n = '0') then
119                 q <= d;
120             end if;
121         end if;
122     end process;
123 end rtl;
124
125
126 --------- 1 bit d-flipflop.
127 library ieee;
128 use ieee.std_logic_1164.all;
129
130 entity d_flip_flop_bit is 
131     port (  
132             clk     : in std_logic;
133             res_n   : in std_logic;
134             set_n   : in std_logic;
135             we_n    : in std_logic;
136             d       : in std_logic;
137             q       : out std_logic
138         );
139 end d_flip_flop_bit;
140
141 architecture rtl of d_flip_flop_bit is
142 begin
143
144     process (clk, res_n, set_n, d)
145     begin
146         if (res_n = '0') then
147             q <= '0';
148         elsif (set_n = '0') then
149             q <= d;
150         elsif (rising_edge(clk)) then
151             if (we_n = '0') then
152                 q <= d;
153             end if;
154         end if;
155     end process;
156 end rtl;
157
158 ----------------------------------------
159 --- tri-state buffer
160 ----------------------------------------
161
162 library ieee;
163 use ieee.std_logic_1164.all;
164
165 entity tri_state_buffer is 
166     generic (
167             dsize : integer := 8
168             );
169     port (  
170             oe_n    : in std_logic;
171             d       : in std_logic_vector (dsize - 1 downto 0);
172             q       : out std_logic_vector (dsize - 1 downto 0)
173         );
174 end tri_state_buffer;
175
176 architecture rtl of tri_state_buffer is
177 begin
178     q <= d when oe_n = '0' else
179         (others => 'Z');
180 end rtl;
181
182 -------------------------------
183 ------ count up registers -----
184 -------------------------------
185 library ieee;
186 use ieee.std_logic_1164.all;
187
188 entity counter_register is 
189     generic (
190         dsize       : integer := 8;
191         inc         : integer := 1
192     );
193     port (  clk         : in std_logic;
194             rst_n       : in std_logic;
195             ce_n        : in std_logic;
196             we_n        : in std_logic;
197             d           : in std_logic_vector(dsize - 1 downto 0);
198             q           : out std_logic_vector(dsize - 1 downto 0)
199     );
200 end counter_register;
201
202 architecture rtl of counter_register is
203
204 use ieee.std_logic_unsigned.all;
205
206
207 begin
208         
209     clk_p : process (clk, rst_n)
210     variable q_out : std_logic_vector(dsize - 1 downto 0);
211     begin
212         if (rst_n = '0') then
213             q_out := (others => '0');
214         elsif (rising_edge(clk)) then
215             if (we_n = '0') then
216                 q_out := d;
217             elsif (ce_n = '0') then
218                 q_out := q_out + inc;
219             end if;
220         end if;
221         
222         --output data...
223         q <= q_out;
224     end process;
225
226 end rtl;
227