Skip to content

Commit a93bda4

Browse files
Alex YazdaniAlex Yazdani
Alex Yazdani
authored and
Alex Yazdani
committed
first commit
0 parents  commit a93bda4

7 files changed

+434
-0
lines changed

4_to_16_decoder.v

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
Alex Yazdani
3+
18 November 2024
4+
4-to-16 Decoder
5+
6+
Inputs:
7+
in (4 bits): Input binary value
8+
enable (1 bit): Enable signal
9+
10+
Outputs:
11+
out (16 bits): One-hot output based on the input value
12+
*/
13+
14+
module decoder_4to16(
15+
input [3:0] in,
16+
input enable,
17+
output reg [15:0] out
18+
);
19+
20+
always @(*) begin
21+
if (enable) begin
22+
out = 16'b0; // Default all outputs to 0
23+
case (in)
24+
4'b0000: out[0] = 1;
25+
4'b0001: out[1] = 1;
26+
4'b0010: out[2] = 1;
27+
4'b0011: out[3] = 1;
28+
4'b0100: out[4] = 1;
29+
4'b0101: out[5] = 1;
30+
4'b0110: out[6] = 1;
31+
4'b0111: out[7] = 1;
32+
4'b1000: out[8] = 1;
33+
4'b1001: out[9] = 1;
34+
4'b1010: out[10] = 1;
35+
4'b1011: out[11] = 1;
36+
4'b1100: out[12] = 1;
37+
4'b1101: out[13] = 1;
38+
4'b1110: out[14] = 1;
39+
4'b1111: out[15] = 1;
40+
default: out = 16'b0;
41+
endcase
42+
end else begin
43+
out = 16'b0; // Outputs are all zero when enable is low
44+
end
45+
end
46+
47+
endmodule

elevator.v

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
Alex Yazdani
3+
18 November 2024
4+
Elevator Controller Module
5+
6+
Inputs:
7+
clk
8+
reset
9+
req_floor (3 bits: floor request)
10+
11+
Outputs:
12+
current_floor (3 bits: current floor)
13+
moving (1 bit: elevator moving indicator)
14+
15+
States:
16+
IDLE
17+
MOVING_UP
18+
MOVING_DOWN
19+
ARRIVED
20+
*/
21+
22+
module elevator_controller(
23+
input clk, reset,
24+
input [2:0] req_floor,
25+
output reg [2:0] current_floor,
26+
output reg moving
27+
);
28+
29+
// State Encoding
30+
parameter IDLE = 2'b00;
31+
parameter MOVING_UP = 2'b01;
32+
parameter MOVING_DOWN = 2'b10;
33+
parameter ARRIVED = 2'b11;
34+
35+
reg [1:0] state, next_state;
36+
37+
// State Memory
38+
always @(posedge clk or posedge reset) begin
39+
if (reset)
40+
state <= IDLE;
41+
else
42+
state <= next_state;
43+
end
44+
45+
// Next State Logic (NSL)
46+
always @(*) begin
47+
next_state = state; // Default to prevent latches
48+
case (state)
49+
IDLE: begin
50+
if (req_floor > current_floor)
51+
next_state = MOVING_UP;
52+
else if (req_floor < current_floor)
53+
next_state = MOVING_DOWN;
54+
end
55+
MOVING_UP: begin
56+
if (current_floor == req_floor)
57+
next_state = ARRIVED;
58+
end
59+
MOVING_DOWN: begin
60+
if (current_floor == req_floor)
61+
next_state = ARRIVED;
62+
end
63+
ARRIVED: next_state = IDLE;
64+
default: next_state = IDLE;
65+
endcase
66+
end
67+
68+
// Output and Floor Logic
69+
always @(posedge clk or posedge reset) begin
70+
if (reset) begin
71+
current_floor <= 3'd0; // Reset to ground floor
72+
moving <= 1'b0;
73+
end else begin
74+
case (state)
75+
MOVING_UP: begin
76+
current_floor <= current_floor + 1;
77+
moving <= 1'b1;
78+
end
79+
MOVING_DOWN: begin
80+
current_floor <= current_floor - 1;
81+
moving <= 1'b1;
82+
end
83+
ARRIVED: moving <= 1'b0;
84+
default: moving <= 1'b0;
85+
endcase
86+
end
87+
end
88+
89+
endmodule

priority_encoder.v

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
Alex Yazdani
3+
18 November 2024
4+
4-to-2 Priority Encoder
5+
6+
Inputs:
7+
in (4 bits): Input signal
8+
Outputs:
9+
out (2 bits): Encoded output
10+
valid (1 bit): Indicates if any input is valid
11+
*/
12+
13+
module priority_encoder(
14+
input [3:0] in,
15+
output reg [1:0] out,
16+
output reg valid
17+
);
18+
19+
always @(*) begin
20+
valid = 1'b0; // Default to no valid input
21+
out = 2'b00; // Default output value
22+
casez (in)
23+
4'b1???: begin
24+
valid = 1'b1;
25+
out = 2'b11;
26+
end
27+
4'b01??: begin
28+
valid = 1'b1;
29+
out = 2'b10;
30+
end
31+
4'b001?: begin
32+
valid = 1'b1;
33+
out = 2'b01;
34+
end
35+
4'b0001: begin
36+
valid = 1'b1;
37+
out = 2'b00;
38+
end
39+
default: begin
40+
valid = 1'b0;
41+
out = 2'b00;
42+
end
43+
endcase
44+
end
45+
46+
endmodule

rca.v

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
Alex Yazdani
3+
18 November 2024
4+
4-bit Ripple Carry Adder
5+
6+
Inputs:
7+
A (4 bits): First operand
8+
B (4 bits): Second operand
9+
Cin (1 bit): Carry-in
10+
11+
Outputs:
12+
Sum (4 bits): Result of A + B + Cin
13+
Cout (1 bit): Carry-out of the most significant bit
14+
*/
15+
16+
module ripple_carry_adder(
17+
input [3:0] A,
18+
input [3:0] B,
19+
input Cin,
20+
output [3:0] Sum,
21+
output Cout
22+
);
23+
24+
wire c1, c2, c3; // Internal carry signals
25+
26+
// Full Adder for bit 0
27+
full_adder FA0 (
28+
.a(A[0]),
29+
.b(B[0]),
30+
.cin(Cin),
31+
.sum(Sum[0]),
32+
.cout(c1)
33+
);
34+
35+
// Full Adder for bit 1
36+
full_adder FA1 (
37+
.a(A[1]),
38+
.b(B[1]),
39+
.cin(c1),
40+
.sum(Sum[1]),
41+
.cout(c2)
42+
);
43+
44+
// Full Adder for bit 2
45+
full_adder FA2 (
46+
.a(A[2]),
47+
.b(B[2]),
48+
.cin(c2),
49+
.sum(Sum[2]),
50+
.cout(c3)
51+
);
52+
53+
// Full Adder for bit 3
54+
full_adder FA3 (
55+
.a(A[3]),
56+
.b(B[3]),
57+
.cin(c3),
58+
.sum(Sum[3]),
59+
.cout(Cout)
60+
);
61+
62+
endmodule
63+
64+
// Full Adder Module
65+
module full_adder(
66+
input a,
67+
input b,
68+
input cin,
69+
output sum,
70+
output cout
71+
);
72+
73+
assign sum = a ^ b ^ cin; // XOR for sum
74+
assign cout = (a & b) | (b & cin) | (a & cin); // Carry-out logic
75+
76+
endmodule

traffic_controller.v

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
Alex Yazdani
3+
18 November 2024
4+
Traffic Controller Module
5+
*/
6+
7+
module traffic_controller(
8+
input clk, reset, ped_req,
9+
output [1:0] veh_light,
10+
output ped_light
11+
);
12+
13+
// State Encoding
14+
parameter IDLE = 2'b00;
15+
parameter GREEN = 2'b01;
16+
parameter YELLOW = 2'b10;
17+
parameter RED = 2'b11;
18+
19+
reg [1:0] state, next_state;
20+
reg [3:0] counter;
21+
22+
// Counter Logic
23+
always @(posedge clk or posedge reset) begin
24+
if (reset) counter <= 0;
25+
else if (counter == 4'd9 && state == GREEN) counter <= 0;
26+
else if (counter == 4'd4 && state == YELLOW) counter <= 0;
27+
else if ((counter == 4'd9 && !ped_req) || (counter > 4'd9)) counter <= 0;
28+
else if (state != next_state) counter <= 0;
29+
else counter <= counter + 1;
30+
end
31+
32+
// State Memory
33+
always @(posedge clk or posedge reset) begin
34+
if (reset) state <= IDLE;
35+
else state <= next_state;
36+
end
37+
38+
// Next State Logic (NSL)
39+
always @(*) begin
40+
next_state = state;
41+
case (state)
42+
IDLE: next_state = GREEN;
43+
GREEN: if (counter == 4'd9) next_state = YELLOW;
44+
YELLOW: if (counter == 4'd4) next_state = RED;
45+
RED: if (((counter == 4'd9) && !ped_req) || (counter > 4'd9)) next_state = GREEN;
46+
default: next_state = IDLE;
47+
endcase
48+
end
49+
50+
// Output Logic (OFL)
51+
assign veh_light = state;
52+
assign ped_light = (state == RED);
53+
54+
endmodule

vending_machine.v

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
Alex Yazdani
3+
18 November 2024
4+
Vending Machine Module
5+
6+
Inputs:
7+
clk
8+
reset
9+
coin
10+
dispense
11+
12+
Outputs:
13+
water_out
14+
status
15+
16+
States:
17+
IDLE
18+
COIN_INSERTED
19+
DISPENSE_WATER
20+
DISPENSE_COMPLETE
21+
*/
22+
23+
module vending_machine(
24+
input clk, reset,
25+
input coin, dispense,
26+
output water_out,
27+
output [1:0] status
28+
);
29+
30+
// State Encoding
31+
parameter IDLE = 2'b00;
32+
parameter COIN_INSERTED = 2'b01;
33+
parameter DISPENSE_WATER = 2'b10;
34+
parameter DISPENSE_COMPLETE = 2'b11;
35+
36+
// Counter Logic
37+
reg [2:0] counter;
38+
always @(posedge clk) begin
39+
if (reset) counter <= 3'd0;
40+
else if (state == DISPENSE_WATER) begin
41+
if (counter == 3'd4) counter <= 3'd0;
42+
else counter <= counter + 1;
43+
end else counter <= 3'd0;
44+
end
45+
46+
// State Memory
47+
reg [1:0] state, next_state;
48+
always @(posedge clk) begin
49+
if (reset) state <= IDLE;
50+
else state <= next_state;
51+
end
52+
53+
// Next State Logic
54+
always @(*) begin
55+
next_state = state;
56+
case (state)
57+
IDLE: if (coin) next_state = COIN_INSERTED;
58+
COIN_INSERTED: if (dispense) next_state = DISPENSE_WATER;
59+
DISPENSE_WATER: if (counter == 3'd4) next_state = DISPENSE_COMPLETE;
60+
DISPENSE_COMPLETE: next_state = IDLE;
61+
default: next_state = IDLE;
62+
endcase
63+
end
64+
65+
// Output Function Logic
66+
assign water_out = (state == DISPENSE_WATER);
67+
assign status = state;
68+
69+
endmodule

0 commit comments

Comments
 (0)