OSDN Git Service

now smb all ok!!
[motonesfpga/motonesfpga.git] / simulation / 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 (clk'event and clk = '1') 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 (clk'event and clk = '1') 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 --- data latch declaration
160 ----------------------------------------
161
162 library ieee;
163 use ieee.std_logic_1164.all;
164
165 entity data_latch is 
166     generic (
167             dsize : integer := 8
168             );
169     port (  
170             clk     : 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 data_latch;
175
176 architecture rtl of data_latch is
177 begin
178
179     process (clk, d)
180     begin
181         if ( clk = '1') then
182             --latch only when clock is high
183             q <= d;
184         end if;
185     end process;
186 end rtl;
187
188 ----------------------------------------
189 --- tri-state buffer
190 ----------------------------------------
191
192 library ieee;
193 use ieee.std_logic_1164.all;
194
195 entity tri_state_buffer is 
196     generic (
197             dsize : integer := 8
198             );
199     port (  
200             oe_n    : in std_logic;
201             d       : in std_logic_vector (dsize - 1 downto 0);
202             q       : out std_logic_vector (dsize - 1 downto 0)
203         );
204 end tri_state_buffer;
205
206 architecture rtl of tri_state_buffer is
207 begin
208     q <= d when oe_n = '0' else
209         (others => 'Z');
210 end rtl;
211
212 -------------------------------
213 ------ count up registers -----
214 -------------------------------
215 library ieee;
216 use ieee.std_logic_1164.all;
217
218 entity counter_register is 
219     generic (
220         dsize       : integer := 8;
221         inc         : integer := 1
222     );
223     port (  clk         : in std_logic;
224             rst_n       : in std_logic;
225             ce_n        : in std_logic;
226             we_n        : in std_logic;
227             d           : in std_logic_vector(dsize - 1 downto 0);
228             q           : out std_logic_vector(dsize - 1 downto 0)
229     );
230 end counter_register;
231
232 architecture rtl of counter_register is
233
234 component d_flip_flop
235     generic (
236             dsize : integer := 8
237             );
238     port (  
239             clk     : in std_logic;
240             res_n   : in std_logic;
241             set_n   : in std_logic;
242             we_n    : in std_logic;
243             d       : in std_logic_vector (dsize - 1 downto 0);
244             q       : out std_logic_vector (dsize - 1 downto 0)
245         );
246 end component;
247
248 use ieee.std_logic_unsigned.all;
249
250 signal dff_we_n : std_logic;
251 signal d_in : std_logic_vector(dsize - 1 downto 0);
252 signal q_out : std_logic_vector(dsize - 1 downto 0);
253
254 begin
255     q <= q_out;
256     dff_we_n <= ce_n and we_n;
257     counter_reg_inst : d_flip_flop generic map (dsize)
258             port map (clk, rst_n, '1', dff_we_n, d_in, q_out);
259         
260     clk_p : process (clk, we_n, ce_n, d)
261     begin
262         if (we_n = '0') then
263             d_in <= d;
264         elsif (ce_n = '0') then
265             d_in <= q_out + inc;
266         end if;
267     end process;
268
269 end rtl;
270