-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoutput_logic.v
More file actions
71 lines (65 loc) · 2.34 KB
/
Copy pathoutput_logic.v
File metadata and controls
71 lines (65 loc) · 2.34 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
module output_logic #(
parameter CURRENCY_WIDTH = 7,
parameter ITEM_ADDR_WIDTH = 10
)(
input wire clk,
input wire rstn,
input wire selection_ready,
input wire currency_ready,
input wire [CURRENCY_WIDTH-1:0] total_currency,
input wire [15:0] item_price,
input wire [7:0] avail_count,
input wire [ITEM_ADDR_WIDTH-1:0] selected_item,
output reg dispense_valid,
output reg [ITEM_ADDR_WIDTH-1:0] item_dispensed,
output reg [CURRENCY_WIDTH-1:0] currency_change,
output reg trigger_dispense
);
// Internal latching flags
reg item_ready_flag;
reg money_ready_flag;
reg [CURRENCY_WIDTH-1:0] latched_currency;
reg [ITEM_ADDR_WIDTH-1:0] latched_item;
always @(posedge clk or negedge rstn) begin
if (!rstn) begin
item_ready_flag <= 0;
money_ready_flag <= 0;
latched_currency <= 0;
latched_item <= 0;
dispense_valid <= 0;
item_dispensed <= 0;
currency_change <= 0;
trigger_dispense <= 0;
end else begin
dispense_valid <= 0;
trigger_dispense <= 0;
// Latch item selection
if (selection_ready) begin
item_ready_flag <= 1;
latched_item <= selected_item;
end
// Latch currency
if (currency_ready) begin
money_ready_flag <= 1;
latched_currency <= total_currency;
end
// Only proceed when both are latched
if (item_ready_flag && money_ready_flag) begin
if (avail_count > 0 && latched_currency >= item_price) begin
dispense_valid <= 1;
item_dispensed <= latched_item;
currency_change <= latched_currency - item_price;
trigger_dispense <= 1;
end else begin
dispense_valid <= 0;
item_dispensed <= latched_item;
currency_change <= latched_currency;
trigger_dispense <= 0;
end
// Clear flags after processing
item_ready_flag <= 0;
money_ready_flag <= 0;
end
end
end
endmodule