Escolar Documentos
Profissional Documentos
Cultura Documentos
Hardware - VERILOG
Definição de funções
Definição de tarefas
Lógica de 4 estados
0 zero, baixo ou falso
1 um, alto ou verdadeiro
z ou Z alta impedancia (flutuante ou 3-state)
x ou X desconhecido ou nao inicializado
Niveis Lógicos em Verilog
Tipos em Verilog
Constantes
Ex.: parameter word = 23, count=50;
Entradas e saidas
input a, b, sel
input [word-1 : 0] addr;
output [7:0] result;
inout [15:0] data;
Ligações e Barramentos
wire a, b, c;
reg [0:8] res; //Armazena o valor em memoria
Comandos e expressões
Adição e subtração: + , -
Multiplicação, divisão e resto: * , /,| %
Operadores a nível de bit: ~ , & , | , ^
Operadores Lógicos: ! , && , ||
Rotação : << , >>
o Outros operadores
Comando if
if ( expr ) cmd else cmd
Comando Case
Case identificador
match : cmd
default : cmd
endcase
Comandos Lógicos para programação
Comando repeat
repeat ( número ) cmd
Comando while
while ( expr ) cmd
Comando for
for ( assignment , expr , assignment ) cmd
Verilog
Comentários em Verilog
// comentário de uma linha só
/* outra forma de comentário de uma linha */
/* inicio de comentário com múltiplas linhas
todo text é ignorado
termina com a linha abaixo*/
Números
decimal, hexadecimal, octal, binario
Decimal sem sinal
Decimal com sinal
Cadeias de caracteres
"Delimite usando aspas numa mesma linha"
limitados a 1024 caracteres
Verilog
Modelos de Descrição
• RTL (Register Transfer Level): descreve o circuito através do que acontece a cada
transição ativa do sinal de relogio
Verilog
Exemplo :
module soma_fluxo_de_dados
(output S, Cout,
input A, B, Cin);
assign S = A ^ B ^ Cin;
assign Cout = (A & B) | (A & Cin) | (B & Cin);
endmodule
VERILOG: Exemplo: Modelo
Comportamental do Multiplexador
Controle de tempo
# delay // atrasa delay unidades de tempo
wait ( expr )
Multiplexador
// um multiplezador de 32 bit 2-to-1 MUX
wire [31:0] mux_out;
assign mux_out = sel ? a : b; //valor inicial
//Portas AND-OR-INVERT
module AOI (input A, B, C, D, output F) ;
assign F = ~ ( (A & B) | (C & D) ) ; // Associação
End module wire out ;
assign out = sel ? a : b ;
Exemplos
// Usando um procedimento
reg out;
always @(a or b or sel) // Usando if/else
begin
reg out;
case ( sel )
always @ ( a or b or sel )
1'b0: out = b ; if ( sel )
1'b1: out = a ; out = a ;
else
endcase
out = b ;
end
Exemplos
module binaryToESeg
(output reg eSeg,
input A, B, C, D);
always @(A, B, C, D)
begin
eSeg = 1;
if (~A & D) if (~A & B & ~C)
eSeg = 0; eSeg = 0;
if (~B & ~C & D)
eSeg = 0;
end
endmodule
Flip Flops
module synDFFwithSetReset
(output reg q, input d, reset, set, clock);
module addsub
(
input [7:0] dataa,
input [7:0] datab,
input add_sub, // if this is 1, add; else subtract
input clk,
output reg [8:0] result begin
); if (add_sub)
result <= dataa + datab;
always @ (posedge clk) else
result <= dataa - datab;
end
End module
Exemplos Contador
module counter
always @(posedge clk or posedge
( reset)
clk, begin
reset, if (reset)
result, result = 0;
ena
); else if (ena)
input clk; result = result + 1;
input reset; end
endmodule
input ena;
output [7:0] result;
reg [7:0] result;
Exemplo DFF
begin //enable
//asynchronous active-low preset else if (ena) q = d;
end endmodule
Exemplo
always @ (state)
begin
// 4-State Moore state machine case (state)
module moore_mac S0: data_out = 2'b01;
( S1: data_out = 2'b10;
S2:data_out = 2'b11;
input clk, data_in, reset,
output reg [1:0] data_out S3:data_out = 2'b00;
); default: data_out = 2'b00;
// Declare state register endcase
reg [1:0]state; end
// Declare states
parameter S0 = 0, S1 = 1, S2 = 2, S3 = 3;
// Output depends only on the state
Maquina de Estados
// Determine the next state S2: if (data_in)
always @ (posedge clk or posedge reset) state <= S3;
begin else
if (reset) state <= S1;
state <= S0; S3:if (data_in)
else state <= S2;
else
case (state)
state <= S3;
S0:state <= S1;
endcase
S1: if (data_in)
end
state <= S2;
else
state <= S1;
RAM
module ram
(
input [7:0] data,
input [5:0] read_addr, write_addr, always @ (posedge clk)
input we, clk, begin
output reg [7:0] q // Write
); if (we)
ram[write_addr] <= data;
// Declare the RAM variable
else
reg [7:0] ram[63:0]; q <= ram[read_addr];
end
endmodule
Memória RAM
module mem_ram_sync(
clk,
rst,
read_rq,
write_rq,
rw_address,
write_data,
read_data
);
input clk;
input rst;
input read_rq;
input write_rq;
input[5:0] rw_address;
input[7:0] write_data;
output[7:0] read_data;
reg[7:0] read_data;
integer out, i;
Memória RAM
// Declare mmory 64x8 bits = 512 bits or 64 bytes
reg [7:0] memory_ram_d [63:0];
reg [7:0] memory_ram_q [63:0];
always @(*)
begin
for (i=0;i<64; i=i+1)
memory_ram_d[i] = memory_ram_q[i];
if (write_rq && !read_rq)
memory_ram_d[rw_address] = write_data;
if (!write_rq && read_rq)
read_data = memory_ram_q[rw_address];
end
endmodule
LCD
LCD
LCD
LCD
LCD
LCD
LCD
LCD
LCD
LCD
LCD
LCD
LCD
LCD
LCD
LCD
LCD
LCD
LCD
LCD
LCD
LCD
LCD
LCD
LCD
LCD
LCD
LCD
LCD
Exercício
Jogo da Velocidade
Fazer um Código em Verilog para:
Identificar o acionamento do botão para ativar a
contagem de 10 segundos para acionamento das chaves
Identificar o acionamento de 5 chaves e ligar cinco LEDs,
identificando qual a ordem de acionamento das chaves.
Identificar o primeiro acionamento (primeira chave a ser
pressionada), acender o LED correspondente. Identificar
a segunda chave acionada, esperar um segundo, acionar
o LED correspondente, e assim sucessivamente até o
último LED.