forked from adam-maj/tiny-gpu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpc.sv
69 lines (61 loc) · 2.41 KB
/
pc.sv
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
`default_nettype none
`timescale 1ns/1ns
// PROGRAM COUNTER
// > Calculates the next PC for each thread to update to (but currently we assume all threads
// update to the same PC and don't support branch divergence)
// > Currently, each thread in each core has it's own calculation for next PC
// > The NZP register value is set by the CMP instruction (based on >/=/< comparison) to
// initiate the BRnzp instruction for branching
module pc #(
parameter DATA_MEM_DATA_BITS = 8,
parameter PROGRAM_MEM_ADDR_BITS = 8
) (
input wire clk,
input wire reset,
input wire enable, // If current block has less threads then block size, some PCs will be inactive
// State
input reg [2:0] core_state,
// Control Signals
input reg [2:0] decoded_nzp,
input reg [DATA_MEM_DATA_BITS-1:0] decoded_immediate,
input reg decoded_nzp_write_enable,
input reg decoded_pc_mux,
// ALU Output - used for alu_out[2:0] to compare with NZP register
input reg [DATA_MEM_DATA_BITS-1:0] alu_out,
// Current & Next PCs
input reg [PROGRAM_MEM_ADDR_BITS-1:0] current_pc,
output reg [PROGRAM_MEM_ADDR_BITS-1:0] next_pc
);
reg [2:0] nzp;
always @(posedge clk) begin
if (reset) begin
nzp <= 3'b0;
next_pc <= 0;
end else if (enable) begin
// Update PC when core_state = EXECUTE
if (core_state == 3'b101) begin
if (decoded_pc_mux == 1) begin
if (((nzp & decoded_nzp) != 3'b0)) begin
// On BRnzp instruction, branch to immediate if NZP case matches previous CMP
next_pc <= decoded_immediate;
end else begin
// Otherwise, just update to PC + 1 (next line)
next_pc <= current_pc + 1;
end
end else begin
// By default update to PC + 1 (next line)
next_pc <= current_pc + 1;
end
end
// Store NZP when core_state = UPDATE
if (core_state == 3'b110) begin
// Write to NZP register on CMP instruction
if (decoded_nzp_write_enable) begin
nzp[2] <= alu_out[2];
nzp[1] <= alu_out[1];
nzp[0] <= alu_out[0];
end
end
end
end
endmodule