-
Notifications
You must be signed in to change notification settings - Fork 0
/
Master.v
114 lines (111 loc) · 2.8 KB
/
Master.v
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
module Master(dataIN,dataOUT,slaveno,enable,reset,cpha,cpol,miso,mosi,clk,cs0,cs1,cs2);
input enable,reset,cpha,cpol,miso,clk;
output reg mosi,cs0,cs1,cs2;
input [1:0] slaveno;
input [7:0] dataIN;
output [7:0] dataOUT;
integer mode = 0;
reg [7:0] data;
assign dataOUT = data;
always @ (posedge clk or negedge clk)
begin
if (clk == 1)
begin
if (reset)
begin
data <= 8'b00000000;
cs0 <= 1'b1;
cs1 <= 1'b1;
cs2 <= 1'b1;
mosi <= 1'b0;
end
else if (enable === 1) begin
case(slaveno)
2'b00 : cs0 <= 1'b0;
2'b01 : cs1 <= 1'b0;
2'b10 : cs2 <= 1'b0;
2'b11 : begin
cs0 <= 1'b1;
cs1 <= 1'b1;
cs2 <= 1'b1;
end
default : begin
cs0 <= 1'b1;
cs1 <= 1'b1;
cs2 <= 1'b1;
end
endcase
if (cpol === 1'b0 && cpha === 1'b0)
mode = 0;
else if (cpol === 1'b0 && cpha === 1'b1)
mode = 1;
else if (cpol === 1'b1 && cpha === 1'b1)
mode = 1;
else if (cpol === 1'b1 && cpha === 1'b0)
mode = 0;
case (mode)
1 : mosi <= data[7];
0 : begin
data [7:1] <= data [6:0];
data [0] <= miso;
end
endcase
end
else if (enable === 1'b0 && dataIN !== 1'b0) begin
data <= dataIN;
cs0 <= 1'b1;
cs1 <= 1'b1;
cs2 <= 1'b1;
end
end
else if(clk == 0)
begin
if (reset)
begin
data <= 8'b00000000;
cs0 <= 1'b1;
cs1 <= 1'b1;
cs2 <= 1'b1;
mosi <= 1'b0;
end
else if (enable === 1'b1) begin
case(slaveno)
2'b00 : cs0 <= 1'b0;
2'b01 : cs1 <= 1'b0;
2'b10 : cs2 <= 1'b0;
2'b11 : begin
cs0 <= 1'b1;
cs1 <= 1'b1;
cs2 <= 1'b1;
end
default : begin
cs0 <= 1'b1;
cs1 <= 1'b1;
cs2 <= 1'b1;
end
endcase
if (cpol === 1'b0 && cpha === 1'b0)
mode = 0;
else if (cpol === 1'b0 && cpha === 1'b1)
mode = 1;
else if (cpol === 1'b1 && cpha === 1'b1)
mode = 1;
else if (cpol === 1'b1 && cpha === 1'b0)
mode = 0;
case (mode)
1 : begin
data [7:1] <= data [6:0];
data [0] <= miso;
end
0 : mosi <= data[7];
endcase
end
else if (enable === 1'b0 && dataIN !== 1'b0) begin
data <= dataIN;
cs0 <= 1'b1;
cs1 <= 1'b1;
cs2 <= 1'b1;
end
end
end
endmodule