-- 6分频为例 architecture a of div is signal clk:std_logic:='0'; signal count:std_logic_vector(2downto0):='000'; begin process(clk_in) --原来的时钟频率 begin if(clk_in'enentand clk_in='1') then if count /=2then--N=6,所以是2,这里是≠2 count=count+1; else clk<= not clk count<='000'; endif; endif; endprocess; clk_out<=clk; end a;
architecture b of div is signal count:std_logic_vector(2downto0):='000'; begin process(clk_in) begin if(clk'eventand clk_in='1')then if count<5then count=count+1; else count = '000'; endif; endif; endprocess; process(count) begin if(count < 3) then clk_out<='0'; else clk_out<='1'; endif; endprocess; end b; -- 占空比为0.5的7分频 3/7 port( clk1,clk2; cnt1,cnt2; ); process(clk_in) begin if(rising_edge(clk1)) then if(cnt<6) then cnt<=cnt+1; else if(cnt<3) then clk='1'; else clk='0';
library IEEE use ieee.std_logic_1164.all entity more is port(clk,rst:instd_logic; state_input:instd_logic; state_output:outstd_logic_vector(1downto0); ) end more; architecture Behaviorial of more is type states is (st0,st1,st2,st3); -- 定义状态枚举类型 signal state:states; begin -- 下一个状态的切换 process(clk,rst) begin if(rst='1')then state<=st0; elsif(clk'eventand clk='1')then case state is when st0=> if state_input='0'then state<=st0;else state<=st1; endif; when st1=> if state_input='0'then state<=st1;else state<=st2; endif; when st2=> if state_input='0'then state<=st2;else state<=st3; endif; when st3=> if state_input='0'then state<=st3;else state<=st0; endif; endcase; endif; endprocess; -- 输出信号 process(state) begin case state is when st0=> state_output<='00'; when st1=> state_output<='01'; when st2=> state_output<='10'; when st3=> state_output<='11'; endcase; endprocess; end Behavioral;
library IEEE use ieee.std_logic_1164.all entity more is port(clk,rst:instd_logic; state_input:instd_logic; state_output:outstd_logic_vector(1downto0); ) end more; architecture Behaviorial of more is type states is(st0,st1,st2,st3); -- 定义状态枚举类型 signal state:states; begin -- 下一个状态的切换 process(clk,rst) begin if(rst='1')then state<=st0; elsif(clk'enentand clk='1')then case state is when st0=> if state_input='0'then state<=st0;else state<=st1; endif; when st1=> if state_input='0'then state<=st1;else state<=st2; endif; when st2=> if state_input='0'then state<=st2;else state<=st3; endif; when st3=> if state_input='0'then state<=st3;else state<=st0; endif; endcase; endif; endprocess; -- 输出信号 process(state) begin case state is when st0=> if state_input='0' state_output<='00';else state_output<='01';endif; when st0=> if state_input='0' state_output<='00';else state_output<='01';endif; when st1=> if state_input='0' state_output<='01';else state_output<='10';endif; when st2=> if state_input='0' state_output<='10';else state_output<='11';endif; when st3=> if state_input='0' state_output<='11';else state_output<='00';endif; endcase; endprocess; end Behavioral;
entity Decoder3to8 is port ( input : instd_logic_vector(2downto0); output : outstd_logic_vector(7downto0); enable: instd_logic ); endentity Decoder3to8;
architecture Behavioral of Decoder3to8 is begin process (input, enable) begin if enable = '0'then case input is when"000" => output <= "00000001"; when"001" => output <= "00000010"; when"010" => output <= "00000100"; when"011" => output <= "00001000"; when"100" => output <= "00010000"; when"101" => output <= "00100000"; when"110" => output <= "01000000"; when"111" => output <= "10000000"; whenothers => output <= "00000000"; endcase; else output <= "00000000"; endif; endprocess; endarchitecture Behavioral;
多路选择器设计(4选一数据选择器)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
-- 端口定义 PORT(A0:INSTD_LOGIC; A1: INSTD_LOGIC; Data:INSTD_LOGIC_VECTOR(3DOWNTO0); EN: INSTD_LOGIC; Y: OUTSTD_LOGIC ); -- 结构体部分: 用WHEN...ELSE语句 Y <= Data(0) WHEN A=“00” ELSE Data(1) WHEN A=“01” ELSE Data(2) WHEN A=“10” ELSE Data(3) WHEN A=“11” ELSE ‘0’; -- 或者也可以用CASE WHEN CASE A IS WHEN “00” => Y <= Data(0); WHEN “01” => Y <= Data(1); WHEN “10” => Y <= Data(2); WHEN “11” => Y <= Data(3); WHEN OTHER Y <= ‘0’;
-- SRAM实现 -- 端口定义 port(address: instd_logic_vector(3downto0); data: inoutstd_logic_vector(7downto0); cs,oe,we:instd_logic ); Architecture behav of ram16*8is subtype word isstd_logic_vector(7downto0); type ram_array isarray(0to15) of word; signal index: inintegerrange0to15; signal sram_store:ram_array; begin index <= CONV_INTEGER(address) process(address,cs,oe,we,data) begin if cs='0'then if we='1'then-- 写入数据 sram_store(index)<=data; elsif oe = '1'then--读出数据 data<=sram_store(index); else data<='zzzzzzzz'; --设置总线为三态 endif; else data<='zzzzzzzz'; endif; endprocess; end behav;
1. 双端口RAM entity dualram is generic( width: positive:=8; -- positive代表≥0的整数 depth: positive:=8 ); port( -- port a 只用来写 clka:instd_logic; wr:instd_logic; addra:instd_logic_vector(depth-1downto0); datain:instd_logic_vector(width-1downto0); -- port b只用来读 clkb:instd_logic; rd:instd_logic; addrb:instd_logic_vector(depth-1downto0); dataout:outstd_logic_vector(width-1downto0) ); endentity dualram;
architecture Behavioral of dualram is type ram isarray(2**depth-1downto0) ofstd_logic_vector(width-1downto0); signal dualram:ram; begin process(clka) begin if clka'enentand clka='1'then if wr='0'then dualram(conv_integer(addra))<=datain; endif; endif; endprocess; process(clkb) begin if clkb'enentand clkb='1'then if rd='0'then dataout<=dualram(conv_integer(addra)); endif; endif; endprocess;
2. 写地址计数器 if rst='0'then wr_pt_t<=(others=>'0'); elsif clk'eventand clk='1'then if wq='0'then wr_pt_t<=wr_pt_t+1; endif; endif; wr_pt<=wr_pt_r
3. 读地址计数器 if rst = '0'then rd_pt_t<=(others=>0); elsif clk'eventand clk='1'then if rq='0'and empty='0'then-- FIFO不为空 rd_pt_t<=rd_pt_t+1; endif; endif; rd_pt<=rd_pt_t; 4. 空满状态产生器 if rst='0'then empty<='1'; elsif clk'enentand clk='1'then if wr_pt=rd_pt then empty<='1'; else empty<='0'; ---- if rst='0'then full<='0'; elsif clk'eventand clk='1'then if wr_pt>rd_pt then if(rd_pt+depth)=wr_pt then full<='1'; else full<='0'; else if (wr_pt+1)=rd_pt then full<='1'; else full<='0';
1 2 3 4 5 6
-- 存储单元数据结构 整数数组: type memory isarray(integerrange<>) ofinteger; 位矢量: subtype word isstd_logic_vector(k-1downto0); type memory isarray(0to2**w-1) of word;