Skip to content

Instantly share code, notes, and snippets.

@jemo07
Last active December 15, 2022 22:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jemo07/5d5ba7d31bb12410888f46ca6060a1f2 to your computer and use it in GitHub Desktop.
Save jemo07/5d5ba7d31bb12410888f46ca6060a1f2 to your computer and use it in GitHub Desktop.
Program Counter in Verilog
// Program Counter (PC)
// The PC is an 8-bit register that stores the address of the command being executed.
// The clock pulse at the positive edge of the clock signal (except mode = 2).
// If mode = 0 reset PC to 0
// If mode = 1, set the PC value to be equal to the value in the data bus.
// If mode = 2, write the PC value to the data bus.
// If mode = 3 do nothing (value z at data bus)
// If mode = 4, increase PC by one unit.
module pc( clock, pc_mode, data_bus, pc_value );
input clock;
input [2:0] pc_mode;
inout [7:0] data_bus;
output [7:0] pc_value;
reg [7:0] pc_value;
reg [7:0] temp;
// This instruction does not wait for the clock signal.
assign data_bus[7:0] = (pc_mode[2:0] == 3'b010 ? pc_value[7:0] : temp[7:0]);
always @(posedge clock)
begin
case( pc_mode[2:0] )
3'b000:
begin
pc_value[7:0] = 8'b0000_0000;
temp[7:0] = 8'bzzzz_zzzz;
end
3'b001:
begin
pc_value[7:0] = data_bus[7:0];
temp[7:0] = 8'bzzzz_zzzz;
end
3'b010:
begin
// do nothing (This mode does not work according to the clock signal.)
end
3'b011:
begin
temp[7:0] = 8'bzzzz_zzzz;
end
3'b100:
begin
pc_value[7:0] = pc_value[7:0] + 1'b1;
temp[7:0] = 8'bzzzz_zzzz;
end
endcase
end
endmodule
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment