Fundamentos de Sistemas Digitais 21/outubro/2018 CIRCUITOS SEQUENCIAIS parte 1 profs. Alexandre M. Amory e Fernando G. Moraes 1
Referências Sugiro estudarem nesta ordem de preferência: Floyd, Cap 7 até 7.4, 9, 10. Não tem nada de VHDL D'AMORE, Roberto. VHDL: Descrição e Síntese de Circuitos Digitais. Rio de Janeiro: LTC, 2005. 259 p. Cap 6 Descrição de Circuitos Síncronos Digital Design and Computer Architecture Sec 2.9, Cap 3, 4, 5 Free Range VHDL Vahid, Cap 3 Parte referente à flip-flop e registradores PEDRONI, Volnei A. Eletrônica Digital Moderna e VHDL. Elsevier Ltda. Editora, Rio de Janeiro, RJ: 2010. 619 p. Teoria: Cap 11 e 12 VHDL: Cap 20 e 21 2
Latch FF Mestre-Escravo 3
Circuitos Combinacionais versus Sequenciais n-inputs Combinational Circuit Combinational Circuit m-outputs (Depend only on inputs) n-inputs Combinational Circuit Next state Storage Elements m-outputs Present state Sequential Circuit 4
LATCH Um latch é um dispositivo de armazenamento É a forma básica de memória A latch S-R (Set-Reset) é o tipo mais básico Pode ser construído a partir de portas NOR ou NAND Com portas NOR, a latch responde a entradas altas Com portas NAND, a latch responde a entradas baixa R Q S Q Notar: uma NOR com uma dada entrada em 0 comporta-se como um inversor. 5
LATCH - NOR O latch com NOR está em uma condição estável (dado armazenado) quando ambas as entradas estão no nível lógico ZERO Assumir inicialmente S e R em 0. Para configurar Q = 1, um sinal ALTO é aplicado à entrada S enquanto o R permanece BAIXO. 0 R 10 0 S 01 Q Q Para RESET, um sinal ALTO é aplicado à entrada R enquanto o S permanece BAIXO. 0 0 R S 01 10 Q Latch initially SET Q Notar: uma NOR com uma dada entrada em 0 comporta-se como um inversor. 6
LATCH - NAND O latch com NAND está em uma condição estável (dado armazenado) quando ambas as entradas estão no nível lógico UM OU Latch initially SET Notar: uma NAND com uma dada entrada em 1 comporta-se como um inversor. 7
LATCH - NAND Latch initially SET 8
Símbolos lógicos para as latches FIGURE 7-4 9
Comportamento da latch 10
Latch com sinal de habilitação Esta latch possui uma entrada adicional, chamada de habilitação (EN) que deve estar em nível ALTO, para que a latch responda às entradas S e R FIGURE 7-8 A gated S-R latch. Uma NAND com uma dada entrada em 0 tem em sua saída 1 è logo S/R iguais a 1 impedindo alterações na saida Q/nQ 11
Latch com sinal de habilitação FIGURE 7-9 12
Latch D com sinal de habilitação A latch D é uma variação do latch S-R, combinando as entradas S e R em uma única entrada D FIGURE 7-10 A gated D latch. Uma regra simples para a latch D: è Q segue D quando o EN está ativo 13
Latch D - comportamento FIGURE 7-11 En ativo : a saída Q é igual è entrada D 0 1 Mantendo En ativo a saída acompanha a entrada 1 D 0 D D 0 1 14
Latch D - comportamento FIGURE 7-11 En ativo : a saída Q é igual è entrada D 1 1 Mantendo En ativo a saída acompanha a entrada 0 D 1 D D 1 0 15
Latch D tabela verdade A tabela de verdade para a latch D é apresentada abaixo à se EN=1, a saída é igual à entrada D à se EN=0 não há alteração na saída, e o dado está armazenado 16
Latch D uma outra implementação Latch D è bit de memória para caches, por exemplo O1 O2 Q1 D ck ck 2 Inversores em série controlados por chaves ck D Q1 CK=0 è saída igual à entrada D Q1 CK=1 è mantém armazenado o último D lido 17
Flip-flop D Mestre-Escravo (MS) Um flip-flop MS difere de uma latch na forma como ele muda de estados Um flip-flop é um dispositivo no qual apenas a borda do relógio determina quando um novo bit é armazenado A sensibilidade à borda pode ser positiva ou negativa Indicação de sensibilidade à borda FIGURE 7-14 Operation of a positive edge-triggered D flip-flop. 18
FFD-MS: Comportamento FIGURE 7-16 19
FFD Mestre Escravo (implementação com chaves) Implementação com utilizando chaves D O1 O2 Q1 Q Q ck ck ck ck CK=0 è Mestre amostra e escravo mantém o estado anterior D Q CK sobe è Informação transferida para a saída D Q 20
LATCH S-R EM VHDL library IEEE; use IEEE.STD_LOGIC_1164.ALL; entity S_R_latch is port ( S, R : in STD_LOGIC; Q, nq : out STD_LOGIC); end S_R_latch ; architecture a1of S_R_latch is signal qq, nqq: STD_LOGIC; begin qq <= R nor nqq; nqq <= S nor qq; nq <= nqq; Q <= qq; Q e notq são também entradas, logo não podem estar em uma saída end a1; 21
library IEEE; use IEEE.STD_LOGIC_1164.ALL; LATCH D EM VHDL entity D_latch is port ( D, ck: in STD_LOGIC; Q, nq : out STD_LOGIC); end D_latch ; architecture a1of D_latch is begin process (ck, D) begin if (ck = '1') then Q <= D; end if; end process; LISTA DE SENSITIVIDADE: Ativa o process quando um dos sinais da lista mudar de valor end a1; 22
FLIP-FLOP D MESTRE-ESCRAVO EM VHDL library IEEE; use IEEE.STD_LOGIC_1164.ALL; entity D_MS is port ( D, ck: in STD_LOGIC; Q, nq : out STD_LOGIC); end D_MS ; architecture a1of D_MS is begin process (ck) begin if rising_edge(clk) then Q <= D; end a1; end if; end process; rising_edge(clk) = clock 'event and clock='1' LISTA DE SENSITIVIDADE: Apenas o ck na lista Observe que o bloco process é sensível a qualquer alteração no valor de ck, no entanto, a função rising_edge (definida na biblioteca IEEE) verifica a alteração do valor quando ocorre uma borda de subida (evento), executando as instruções do bloco if. 23
Registradores Contadores 24
REGISTRADOR FIGURE 7-35 Example of flip-flops used in a basic register for parallel data storage. 25
REGISTRADOR registradores são basicamente sinais declarados em processos com sinal de sincronismo (exemplo: clock). para efeito de síntese e simulação, é aconselhável introduzir um reset assíncrono. process (clock, reset) begin if reset = '1' then reg <= (others => 0 ); elsif clock 'event and clock='1' then reg <= barramento_a; end if; end process; -- portável; è Como introduzir um sinal de enable no registrador, para habilitar a escrita? 26
REGISTRADOR Registrador com largura de palavra parametrizável, com ce : library... entity regnbit is generic(n : integer := 16); port( ck, rst, ce : in std_logic; D : in STD_LOGIC_VECTOR (N-1 downto 0); Q : out STD_LOGIC_VECTOR (N-1 downto 0) ); end regnbit; generic define um parâmetro do módulo architecture regn of regnbit is begin process(ck, rst) begin if rst = '1' then Q <= (others => '0'); elsif ck'event and ck = '0' then if ce = '1' then Q <= D; end if; end if; end process; end regn; Uso: rx: entity work.regnbit generic map(8) port map(ck => ck, rst => rst, ce => ce, D => D, Q => Q); 27
Simulando o Registrador reg1: entity work.regnbit generic map(8) port map(ck => ck, rst => reset, ce => ce, D => D, Q => Q1); reset <= '1', '0' after 5 ns; ck <= not ck after 20 ns; D <= x"aa", x"11" after 18 ns, x"20" after 22 ns, x"31" after 24 ns, x"40" after 26 ns, x"51" after 30 ns, x"60" after 34 ns, x"71" after 36 ns, x"80" after 45 ns, x"91" after 48 ns, x"a0" after 58 ns, x"b1" after 70 ns, x"c0" after 75 ns, x"d1" after 89 ns, x"e0" after 90 ns, x"f1" after 110 ns, x"00" after 170 ns, x"ee" after 230 ns; ce <= '1'; Time ck=1 ce=1 rst=0 d[7:0]=a0 q[7:0]=a0 0 100 ns 200 ns AA 11 + 40 51 + 71 + 91 A0 B1 C0 E0 F1 00 EE 00 11 A0 E0 F1 00 sensibilidade à borda de subida do clock tempo de setup respeitado 28
REGISTRADOR DE DESLOCAMENTO exemplo de registrador de deslocamento: process (clock, reset) begin if reset = '1' then A <= 0; B <= 0; C <= 0; elsif clock'event and clock='1' then A <= entrada; B <= A; C <= B; end if; end process; 1) Desenhe o circuito acima utilizando flip-flops 2) A ordem das atribuições (A,B,C) é importante? O que ocorreria se fosse uma linguagem de programação tipo C? 3) Escreva o código para um registrador com deslocamento à esquerda e a direita 29
REGISTRADOR DE DESLOCAMENTO Atribuição dentro/fora de process: process (clock, reset) begin if clock'event and clock='1' then A <= entrada; B <= A; Conclusão: end if; end process; X <= B + C; C <= B; Y <= B + C; -- fora do process -- dentro do process Qual a diferença de comportamento nas atribuições à X e a Y? - sinais atribuídos em processos, com controle de clock, são flip-flops/registradores. - Sinais fora de processos ou em processos sem variável de sincronismo (clock) serão sintetizados com lógica combinacional. 30
REGISTRADOR DE DESLOCAMENTO SIMULAÇÃO ck=1 entrada[3:0]=3 a[3:0]=3 b[3:0]=2 c[3:0]=0 rst=0 y[3:0]=0 x[3:0]=2 2 3 7 8 A 3 0 2 3 7 8 A 3 0 2 3 7 8 A 0 2 3 7 8 Registrador de deslocamento 0 2 5 A F 0 2 5 A F 2 Y: soma deslocada 1 ciclo de clock X: soma atual process (clock, reset) begin if clock'event and clock='1' then A <= entrada; B <= A; C <= B; Y <= B + C; -- dentro do process end if; end process; X <= B + C; -- fora do process 31
REGISTRADOR formas de entrada de dados FIGURE 8-2 Basic data movement in shift registers. (Four bits are used for illustration. The bits move in the direction of the arrows.) 32
REGISTRADOR carga serial 33
REGISTRADOR carga serial FIGURE 8-4 34
REGISTRADOR CARGA PARALELA E SERIAL 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 library IEEE; use IEEE.STD_LOGIC_1164.ALL; use IEEE.Std_Logic_unsigned.all; entity serial_in is generic(n : integer := 8); port( clk, load, reset : in std_logic; sin : in std_logic; sout : out std_logic; d : in STD_LOGIC_VECTOR (N-1 downto 0); q : out STD_LOGIC_VECTOR (N-1 downto 0) ); end serial_in; architecture a1 of serial_in is signal s: STD_LOGIC_VECTOR (N-1 downto 0); begin process (clk, reset) begin if reset = '1' then s <= (others=>'0'); elsif rising_edge(clk) then if load = '1' then s <= d; else s <= s(n-2 downto 0) & sin; end if; end if; end process; q <= s; sout <= s(n-1); end a1; saída paralela saída serial carga paralela carga serial 35
REGISTRADOR CARGA PARALELA E SERIAL DD <= x"aa"; sin <= '0'; load <= '1', '0' after 30 ns, '1' after 330 ns, '0' after 350 ns; Time reset=0 clk=1 d[7:0]=aa AA load=0 sin=0 sout=0 0 100 ns 200 ns 300 ns 400 ns 500 ns 600 ns carga paralela q[7:0]=00 00 AA 54 A8 50 A0 40 80 00 AA 54 A8 50 A0 40 80 q[0]=0 q[2]=0 q[1]=0 q[3]=0 q[4]=0 q[5]=0 q[6]=0 q[7]=0 carga serial 36
CONTADOR 2 n estados entity contup is port ( end contup; clock, reset, Load, Enable: In std_logic; DATABUS : In Std_logic_Vector (5 downto 0); Upcount2 : Out Std_logic_Vector (5 downto 0)); architecture RTL of contup is Signal Upcount : std_logic_vector (5 downto 0); begin Upcount2 <= Upcount; Upcounter : Process (clock, reset) begin if reset = '1' then Upcount <= "000000"; elsif clock'event and clock='1' then if ENABLE = '1' then if LOAD = '1' end if; end if; end if; end process Upcounter; end RTL; then Upcount <= DATABUS; else Upcount <= Upcount + 1; (1) Determine o comportamento deste contador, fazendo um diagrama de tempos. (2) O reset é prioritário em relação ao clock? Por quê? (3) Como modificar o contador para realizar contagem crescente/decrescente? 37
CONTADOR GRAY 2 n estados Código gray: sequência onde de um estado para outro há apenas a variação de um bit: 000 à 001 à 011 à 010 à 110 à 111 à 101 à 100 à 000 à Uma forma de implementar este código, que não apresenta uma sequência regular, é utilizar uma técnica tipo máquina de estados, onde em função do estado atual do contador, determina-se o próximo estado. architecture RTL of graycounter is signal clock, reset : std_logic;signal graycnt : std_logic_vector (2 downto 0); begin gray : process (clock,reset) begin if reset = '1' then graycnt <= "000"; -- reset assíncrono elsif clock event and clock= 1 then case graycnt is when "000" => graycnt <= "001"; when "001" => graycnt <= "011"; when "010" => graycnt <= "110"; when "011" => graycnt <= "010"; when "100" => graycnt <= "000"; when "101" => graycnt <= "100"; when "110" => graycnt <= "111"; when "111" => graycnt <= "101"; when others => null; end case; end if; end process gray; end RTL; FIGURE 9-26 State diagram for a 3-bit Gray code counter. 38
CONTADOR Johnson 2n estados Contador JOHNSON, utilizando um registrador de deslocamento: if reset = '1' then john <= "0000"; elsif clock event and clock= 1 then end if; john <= john(2 downto 0) & not (john(3)); -- CONCATENAÇÃO 39
EXERCÍCIO 1 Quando o sinal de reset for 1, os registradores R1 e R2 armazenam 0001 e 0000 respectivamente. Determinar o conteúdo de R1 e R2 para os 6 primeiros ciclos de relógio. reset clock reset clock R1 (4 BITS) R2 (4 BITS) somador 4bits Descreva este circuito em VHDL port(clock, reset: in std_logic; saida : out std_logic_vector (N-1 downto 0) ); Na arquitetura (1) Process para R1/R2 (2) soma <= R1 + R2 saída Incluir: library IEEE; use IEEE.STD_LOGIC_1164.ALL; use IEEE.Std_Logic_unsigned.all; 40
EXERCÍCIO 1 reset clock reset clock R1 (4 BITS) R2 (4 BITS) somador 4bits saída clock=1 reset=0 r1[3:0]=d r2[3:0]=8 saida[3:0]=5 1 2 3 5 8 D 0 1 2 3 5 8 1 2 3 5 8 D 5 R1: 1 1 2 3 R2: 0 1 1 2 Saída: 1 2 3 5 41
EXERCÍCIO 1 - Test Bench library IEEE; use IEEE.std_logic_1164.all; entity tb is end tb; architecture arch of tb is signal reset: std_logic; signal ck : std_logic := '0' ; signal saida1: STD_LOGIC_VECTOR(3 downto 0); begin reset <= '1', '0' after 5 ns; ck <= not ck after 20 ns; ex1: entity work.exercicio1 generic map(4) port map(clock => ck, reset => reset, saida => saida1); end arch; 42
EXERCÍCIO 2 (1/4) Descreva o circuito abaixo em VHDL: Um só processo para opa e opb, pois as variáveis de controle são as mesmas reset clock Contador de 8 bits opa opb somador 8bits reset clock Reg. 8 bits 8 saída port(clock, reset: in std_logic; saida : out std_logic_vector (N-1 downto 0) ); 43
EXERCÍCIO 2 (2/4) reset clock Contador 8 bits opa opb somador 8bits reset clock Reg. 8 bits Time reset=0 clock=0 opa[7:0]=07 opb[7:0]=15 soma[7:0]=1c 0 100 ns 200 ns 300 ns 00 01 02 03 04 05 06 07 + + + + 00 01 03 06 0A 0F 15 00 01 03 06 0A 0F 15 1C 8 saída opa: 0 1 2 3 4 5 6 7 opb: 0 1 3 6 10 15 21 28 44
EXERCÍCIO 2 - Test bench (3/4) library IEEE; use IEEE.std_logic_1164.all; entity tb is end tb; architecture arch of tb is signal reset : std_logic; signal ck : std_logic := '0' ; signal saida2: STD_LOGIC_VECTOR(7 downto 0); begin ex2: entity work.exercicio2 generic map(8) port map(clock => ck, reset => reset, saida => saida2); reset <= '1', '0' after 5 ns; ck <= not ck after 20 ns; end arch; 45
EXERCÍCIO 2 (descrição completa) (4/3) library IEEE; use IEEE.Std_Logic_1164.all; use IEEE.Std_Logic_unsigned.all; entity exercicio2 is generic(n : integer := 8); port( clock, reset : in std_logic; saida : out STD_LOGIC_VECTOR (N-1 downto 0) ); end exercicio2; architecture a1 of exercicio2 is signal opa, opb, soma: STD_LOGIC_VECTOR (N-1 downto 0); begin Contador de 8 bits opa somador 8bits soma Reg. 8 bits opb process (clock, reset) begin if reset = '1' then opa <=... opb <=... elsif rising_edge(clock) then opa <=... opb <=... end if; end process; 8 saída soma <= opa + opb saida <= soma; end a1; 46
EXERCÍCIO 3 Divisor de clock (1/2) Qual a saída esperada para fout? 47
EXERCÍCIO 3 Divisor de clock test bench (2/3) library IEEE; use IEEE.std_logic_1164.all; entity tb is end tb; architecture arch of tb is signal reset : std_logic; signal fin : std_logic := '0' ; signal fout: std_logic; begin ex2: entity work.divisor port map(fin => fin, rst => reset, fout => fout); reset <= '1', '0' after 5 ns; fin <= not fin after 10 ns; end arch; 48