Last active
October 30, 2019 02:17
-
-
Save juliafealves/839b7efcafb119bd58be4c69698cfbe5 to your computer and use it in GitHub Desktop.
Máquina de Estados: Problema do Ar Condicionado (https://github.com/OpenDevUFCG/Tamburetei/blob/master/loac/leites/maquinasDeEstados/arcondicionado.md)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// DESCRIPTION: Verilator: Systemverilog example module | |
// with interface to switch buttons, LEDs, LCD and register display | |
parameter NINSTR_BITS = 32; | |
parameter NBITS_TOP = 8, NREGS_TOP = 32, NBITS_LCD = 64; | |
module top(input logic clk_2, | |
input logic [NBITS_TOP-1:0] SWI, | |
output logic [NBITS_TOP-1:0] LED, | |
output logic [NBITS_TOP-1:0] SEG, | |
output logic [NBITS_LCD-1:0] lcd_a, lcd_b, | |
output logic [NINSTR_BITS-1:0] lcd_instruction, | |
output logic [NBITS_TOP-1:0] lcd_registrador [0:NREGS_TOP-1], | |
output logic [NBITS_TOP-1:0] lcd_pc, lcd_SrcA, lcd_SrcB, | |
lcd_ALUResult, lcd_Result, lcd_WriteData, lcd_ReadData, | |
output logic lcd_MemWrite, lcd_Branch, lcd_MemtoReg, lcd_RegWrite); | |
parameter TEMPERATURA_MINIMA = 0; | |
parameter TEMPERATURA_MAXIMA = 7; | |
parameter INICIALIZA_PINGAR = 10; | |
parameter CLOCKS_PARAR_PINGAR = 4; | |
enum logic [2:0] { refrigerando, aumentando, diminuindo } estado; | |
// tem frequência de 1 Hz. | |
logic clk_1; | |
// reset assincrono. | |
logic reset; | |
// para diminuir a temperatura desejada em 1 grau Celsius. | |
logic diminuir; | |
// para aumentar a temperatura desejada em 1 grau Celsius. | |
logic aumentar; | |
// indica que o ar-condicionado está gotejando atualmente. | |
logic pingando; | |
// representa a temperatura real para o ambiente. 20[000] e 27[111] graus Celsius (inclusive). | |
logic [2:0] temperatura_real; | |
// representa a temperatura desejada para o ambiente. 20[000] e 27[111] graus Celsius (inclusive). | |
logic [2:0] temperatura_desejada; | |
// Contador dos clocks para começar a pingar | |
logic [4:0] contador_pingando; | |
// Contador para parar o pingamento | |
logic [2:0] parar_pingar; | |
always_comb begin | |
// entradas | |
diminuir <= SWI[0]; | |
aumentar <= SWI[1]; | |
reset <= SWI[7]; | |
// saídas | |
SEG[7] <= clk_1; | |
LED[2:0] <= temperatura_desejada; | |
LED[3] <= (contador_pingando == INICIALIZA_PINGAR && parar_pingar != CLOCKS_PARAR_PINGAR); | |
LED[6:4] <= temperatura_real; | |
lcd_a <= estado; | |
end | |
always_ff @(posedge clk_2) clk_1 <= !clk_1; | |
always_ff @(posedge clk_1 or posedge reset) begin | |
if(reset) begin | |
temperatura_desejada <= 0; | |
temperatura_real <= 0; | |
contador_pingando <= 0; | |
parar_pingar <= 0; | |
estado <= refrigerando; | |
end else begin | |
unique case(estado) | |
refrigerando: begin | |
if(diminuir && temperatura_desejada > TEMPERATURA_MINIMA) begin | |
temperatura_desejada <= temperatura_desejada - 1; | |
estado <= diminuindo; | |
end else if(aumentar && temperatura_desejada < TEMPERATURA_MAXIMA) begin | |
temperatura_desejada <= temperatura_desejada + 1; | |
estado <= aumentando; | |
end else estado <= refrigerando; | |
end | |
diminuindo: begin | |
if(temperatura_real > temperatura_desejada) temperatura_real <= temperatura_real - 1; | |
else temperatura_real <= temperatura_desejada; | |
estado <= refrigerando; | |
end | |
aumentando: begin | |
if(temperatura_real < temperatura_desejada) temperatura_real <= temperatura_real + 1; | |
else temperatura_real <= temperatura_desejada; | |
estado <= refrigerando; | |
end | |
endcase | |
// O ar-condicionado começa a pingar 10 ciclos de clock após o reset. | |
if(contador_pingando < INICIALIZA_PINGAR) begin | |
contador_pingando <= contador_pingando + 1; | |
end | |
// Para interromper o gotejamento, o usuário deve ter a temperatura real em 27 graus Celsius por, pelo menos, 4 ciclos de clock. | |
if(temperatura_real == TEMPERATURA_MAXIMA && parar_pingar < CLOCKS_PARAR_PINGAR) begin | |
parar_pingar <= parar_pingar + 1; | |
end | |
end | |
end | |
endmodule |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment