-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdesign_m.v
More file actions
179 lines (141 loc) · 3.81 KB
/
design_m.v
File metadata and controls
179 lines (141 loc) · 3.81 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
//personal encoder
module compression(
input i_clk,
input c14,
//input wire[15:0] i_bits,
output wire[7:0] o_bits
);
//intermdiate wires
wire[15:0] k_add_out;
wire[2:0] p_out_;
wire[3:0] lsls_out_;
wire ack;
wire[15:0] add_in;
wire[15:0] add_out;
wire[2:0] p_out;
wire[3:0] lsls_out;
//intermediate registers
reg[15:0] pcm_data; //have to give it some initial value
reg[15:0] r_add_in;
reg[15:0] r_add_out;
reg[2:0] r_p_out = 0;
reg[3:0] r_lsls_out = 0;
reg[7:0] r_out = 0;
//instantiatiate modules & all are combinational logic(pipelining into '4' stages)
k_bit_adder k_add_inst(.i_bits(add_in), .o_bits(k_add_out));
priority_encoder p_encoder_inst(.i_bits(add_out[14:5]), .o_bits(p_out_));
lsls_8 lsls_inst(.i_bits(k_add_out), .shift_bits(p_out), .o_bits(lsls_out_));
//with clock
uart_tx uart_tx_inst(.C14(c14), .i_clk(i_clk), .ack(ack), .data(pcm_data));
reg[2:0] lat_c = 0;
reg[1:0] state = 2'b00;
parameter idle = 2'b00;
parameter start = 2'b01;
always @(posedge i_clk) begin
case(state)
idle: begin
if(ack == 1'b1) state <= start;
end
start: begin //assume all combinational takes one clock cycle..(latency = 3)
r_add_in <= pcm_data;
r_add_out <= k_add_out;
r_p_out <= p_out_;
r_lsls_out <= lsls_out_;
if(lat_c != 4) lat_c = lat_c + 1;
else begin
lat_c = 0;
r_out = {8{pcm_data[15]}}^{1'b0, p_out, lsls_out};
end
if(ack == 1'b0) state <= idle;
end
endcase
end
assign add_in = r_add_in;
assign add_out = r_add_out;
assign p_out = r_p_out;
assign lsls_out = r_lsls_out;
//output:
assign o_bits = r_out;
endmodule
//zync-700 runs at 100MGHz, uart_bits
//baud rate: 115200, 8 bits : need to wait 868 cycles,
//I only care about C14(TX)
module uart_tx(
input C14,
input i_clk,
output wire ack,
output reg[15:0] data
);
initial begin
data = 16'h000; //for the first value
end
reg r_ack = 0;
reg[1:0] state = 0;
reg[9:0] counter = 0;
reg[1:0] parts = 0;
reg[4:0] index= 0;
parameter idle = 2'b00;
parameter start = 2'b01;
parameter sample = 2'b10;
parameter stop = 2'b11;
always @(posedge i_clk) begin
case(state)
idle:begin
if(C14 == 1'b0) state <= start;
end
//goal sample in the middle
start:begin
if(counter != 434) counter <= counter + 1;
else begin
counter <= 0;
state <= sample;
r_ack <= 1'b0; //new sample starting so have to restart;
end
end
sample: begin
if(counter != 868) begin
counter <= counter + 1;
end
//sampling part
//break it up
else if(((index != 8) || (parts != 0)) && (index != 16)) begin
data[(15-index)] <= C14;
index <= index + 1;
counter <= 0;
end
//will fail when index = 16, and index = 8 and parts = 0
else begin
state <= stop;
end
end
stop: begin
if(C14 == 1'b1) state <= idle;
if(parts != 1) begin
parts <= parts + 1;//
end
else begin
parts <= 0;
index <= 0;
r_ack <= 1'b1;
end
counter <= 0;
end
endcase
end
assign ack = r_ack; //register is ready to read
endmodule
module priority_encoder(
input wire[9:0] i_bits,
output wire[2:0] o_bits
//outputwire[7:0] bits_,
//output wire im123_,
// output wire im567_
);
reg[2:0] r_bits;
always@(*) begin
r_bits[0] = ((i_bits[0] | i_bits[1]) & ~(i_bits[2])) | (i_bits[3] & ~(i_bits[4])) | (i_bits[5] & ~i_bits[6]) | i_bits[9:7];
r_bits[1] = ((i_bits[2] | i_bits[3]) & ~(i_bits[4] | i_bits[5])) | (i_bits[9:7]);
r_bits[2] = |(i_bits[9:4]);
end
assign o_bits = r_bits;
endmodule