15 martie 2010

Comanda semaforului de trafic

// Model numeric pentru un controlul unui semafor de trafic.
// By Dan Hyde,
module traffic;
parameter on = 1, off = 0, red_tics = 35, amber_tics = 3, green_tics = 20;
reg clock, red, amber, green;
// va opri simularea dupa 1000 unitati de timp
initial begin: stop_at
#1000; $stop;
end
// initializeaza luminile si seteaza monitorizarea registrelor.
initial begin: Init
red = off; amber = off; green = off;
$display(" Timpul green amber red");
$monitor("%3d %b %b %b", $time, green, amber, red);
$vw_dumpvars();
$vw_group("all", $time, green, amber, red);
end
// task de asteptare pentru aparitiile fronturilor pozitive ale ceasului
// inainte de a stinge (off) luminile
task light;
output color;
input [31:0] tics;
begin
repeat(tics) // asteapta detectarea fronturilor pozitive ale ceasului
@(posedge clock);
color = off;
end
endtask
// forma de unda pentru o un ceas cu o perioada de doua unitati de timp
always begin: clock_wave
#1 clock = 0;
#1 clock = 1;
end
always begin: main_process
red = on;
light(red, red_tics); // cheama task-ul de asteptare
green = on;
light(green, green_tics);
amber = on;
light(amber, amber_tics);
end
endmodule


Iesirea este urmatoare

(Time green amber red)
0 0 0 1
70 1 0 0
110 0 1 0
116 0 0 1
186 1 0 0
226 0 1 0
232 0 0 1
302 1 0 0
342 0 1 0
348 0 0 1
418 1 0 0
458 0 1 0
464 0 0 1
534 1 0 0
574 0 1 0
580 0 0 1
650 1 0 0
690 0 1 0
696 0 0 1
766 1 0 0
806 0 1 0
812 0 0 1
882 1 0 0
922 0 1 0
928 0 0 1
998 1 0 0

Niciun comentariu: