hashirehtisham commited on
Commit
7b7b932
·
verified ·
1 Parent(s): 334d5e2

Update verilog.txt

Browse files
Files changed (1) hide show
  1. verilog.txt +78 -120
verilog.txt CHANGED
@@ -1,122 +1,80 @@
1
- `timescale 1ns / 1ps
2
-
3
- module alu(
4
- // Data inputs
5
- input [63:0] a , // 64 bit input 1
6
- input [63:0] b , // 64 bit input 2
7
- input [4:0] alu_control , // 4 bit opcode
8
- input [63:0] hi_in , // Current value of Hi
9
- input [63:0] lo_in , // Current value of Lo
10
- input [4:0] shamt , // Shift amount
11
-
12
- // Result outputs
13
- output reg [63:0] result , // 64 bit result of alu
14
- output reg [63:0] hi_out ,
15
- output reg [63:0] lo_out ,
16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  // Status signals back to control unit
18
- output reg zero , // 1 bit Zero flag
19
- output reg neg , // neg flag
20
- output reg carry , // carry flag (answer too big for bits)
21
- output reg overflow // overflow flag (wrong sign)
22
- );
23
- reg [16:0] temp_scaler;
24
- always @ (*)
25
- begin
26
- result = 64'd0;
27
- zero = 0;
28
- carry = 0;
29
- overflow = 0;
30
- neg = 0;
31
- hi_out = hi_in;
32
- lo_out = lo_in;
33
-
34
- case (alu_control)
35
- 5'b00000,5'b00101,5'b00111,5'b01000 : begin // group 1 (scaler add)
36
- // addition of two 16 bit no leads to 17 bit answer , alot that 17th bit to carry
37
- // That 17th bit
38
- {carry , result[15:0]} = a[15:0] + b[15:0];
39
-
40
- // Checking for overflow (comparing signs)
41
- if ((a[15] == b[15]) && (result[15] != a[15]))
42
- overflow = 1;
43
- end
44
-
45
- 5'b01111,5'b10001: begin // Group 2 (vector addition) (SIMD)
46
- result[15:0] = a[15:0] + b[15:0];
47
- result[31:16] = a[31:16] + b[31:16];
48
- result[47:32] = a[47:32] + b[47:32];
49
- result[63:48] = a[63:48] + b[63:48];
50
- // assigning the 64th bit to neg flag if 64th bit = 0 (positive) if 64th bit =1 (neg)
51
- neg = result[63];
52
- end
53
- 5'b01101: begin // Scaler multiplication
54
- // Result of multiplication gets stored into first 32 bits of hi and lo out regs
55
- {hi_out[31:0] , lo_out[31:0]} = a[31:0] * b[15:0];
56
- // setting the last 32 bits to 0
57
- hi_out[63:32] = 32'd0;
58
- lo_out[63:32] = 32'd0;
59
-
60
- end
61
- 5'b10000:begin // Vector multiplication
62
- result[31:0] = a[15:0] * b[15:0];
63
- result[63:32] = a[31:16] * b[31:16];
64
- // checking neg again
65
- neg = result[63];
66
- end
67
-
68
- 5'b01010 , 5'b01011 , 5'b10100: begin
69
- // Scaler subtraction , here 10100 is custom opcade made by ourselves because for the alu to understand the instruction we still need it
70
- temp_scaler = a[15:0] + ~(b[15:0]) +1'b1;
71
- result[15:0] = temp_scaler[15:0];
72
- result[63:16] = 48'd0;
73
-
74
- neg = result[15];
75
-
76
- if ((a[15] == b[15]) && (result[15] != a[15]));
77
- overflow = 1;
78
- end
79
- // Logical operations
80
- // AND
81
- 5'b00100: begin
82
- result = a & b;
83
- neg = result[63];
84
- end
85
- // OR
86
- 5'b00011: begin
87
- result = a | b;
88
- neg = result[63];
89
- end
90
- // Shift left logical
91
- 5'b00001: begin
92
- result = a << shamt;
93
- neg = result[63];
94
- end
95
- // Shift right logical
96
- 5'b00010: begin
97
- result = a >> shamt;
98
- neg = result[63];
99
- end
100
- // move from lo
101
- 5'b01110: begin
102
- result = lo_in;
103
- end
104
- // move from hi
105
- 5'b01100: begin
106
- result = hi_in;
107
- end
108
- // load immediate
109
- 5'b00110, 5'b10010:result = b;
110
-
111
- default: result = 64'd0;
112
- endcase
113
-
114
- // Set zero flag
115
- if (result == 64'd0)
116
- zero = 1;
117
- else
118
- zero = 0;
119
- end
120
-
121
-
122
- endmodule
 
1
+ [Author info]
2
+ - Hashir Ehtisham: NUST student, made this chatbot for ICT project.
 
 
 
 
 
 
 
 
 
 
 
 
 
3
 
4
+ This is an ALU module of the code for processor.
5
+ [ALU Module]
6
+ Line 1 of ALU module:
7
+ `timescale 1ns / 1ps
8
+ Explanation: Sets the simulation time step to 1 nanosecond with a precision of 1 picosecond.
9
+
10
+ Line 2 of ALU module:
11
+ module alu(
12
+ Explanation: Starts the hardware definition for the Arithmetic Logic Unit (ALU).
13
+
14
+ Line 3 of ALU module:
15
+ // Data inputs
16
+ Explanation: Comment identifying the start of the input data signals.
17
+
18
+ Line 4 of ALU module:
19
+ input [63:0] a ,
20
+ Explanation: 64-bit input port for the first operand (Operand A).
21
+
22
+ Line 5 of ALU module:
23
+ input [63:0] b ,
24
+ Explanation: 64-bit input port for the second operand (Operand B).
25
+
26
+ Line 6 of ALU module:
27
+ input [4:0] alu_control ,
28
+ Explanation: 5-bit control signal that determines which operation (Add, Sub, etc.) to perform.
29
+
30
+ Line 7 of ALU module:
31
+ input [63:0] hi_in ,
32
+ Explanation: 64-bit input to read the current state of the High-order result register.
33
+
34
+ Line 8 of ALU module:
35
+ input [63:0] lo_in ,
36
+ Explanation: 64-bit input to read the current state of the Low-order result register.
37
+
38
+ Line 9 of ALU module:
39
+ input [4:0] shamt ,
40
+ Explanation: 5-bit input specifying the number of bits to shift during shift operations.
41
+
42
+ Line 10 of ALU module:
43
+ // Result outputs
44
+ Explanation: Comment identifying the start of the primary data output signals.
45
+
46
+ Line 11 of ALU module:
47
+ output reg [63:0] result ,
48
+ Explanation: 64-bit register output holding the main calculation result.
49
+
50
+ Line 12 of ALU module:
51
+ output reg [63:0] hi_out ,
52
+ Explanation: 64-bit register output for high-order bits (used in multiplication/division).
53
+
54
+ Line 13 of ALU module:
55
+ output reg [63:0] lo_out ,
56
+ Explanation: 64-bit register output for low-order bits (used in multiplication/division).
57
+
58
+ Line 14 of ALU module:
59
  // Status signals back to control unit
60
+ Explanation: Comment identifying status flags sent to the CPU control logic.
61
+
62
+ Line 15 of ALU module:
63
+ output reg zero ,
64
+ Explanation: 1-bit flag that is high (1) if the result of the operation is zero.
65
+
66
+ Line 16 of ALU module:
67
+ output reg neg ,
68
+ Explanation: 1-bit flag that is high (1) if the result is a negative value.
69
+
70
+ Line 17 of ALU module:
71
+ output reg carry ,
72
+ Explanation: 1-bit flag indicating an unsigned arithmetic carry-out.
73
+
74
+ Line 18 of ALU module:
75
+ output reg overflow
76
+ Explanation: 1-bit flag indicating a signed arithmetic overflow (result sign error).
77
+
78
+ Line 19 of ALU module:
79
+ );
80
+ Explanation: Closes the module's port list declaration.