data/full_repos/permissive/169266176/bootstrap.v
1: b"%Error: data/full_repos/permissive/169266176/bootstrap.v:116: Cannot find file containing module: 'spiflash'\nspiflash flash(\n^~~~~~~~\n        ... Looked in:\n             data/full_repos/permissive/169266176,data/full_repos/permissive/169266176/spiflash\n             data/full_repos/permissive/169266176,data/full_repos/permissive/169266176/spiflash.v\n             data/full_repos/permissive/169266176,data/full_repos/permissive/169266176/spiflash.sv\n             spiflash\n             spiflash.v\n             spiflash.sv\n             obj_dir/spiflash\n             obj_dir/spiflash.v\n             obj_dir/spiflash.sv\n%Error: Exiting due to 1 error(s)\n"
module bootstrap(
	 
	 output wire flash_cs,
	 output wire flash_ck,
	 output wire flash_si,
	 input wire flash_so,
 	 output cs0,
	 output cs1,
	 output reg rw_out,
	 output reg [15:0] addr_out,
	 output [5:0] addr_ext,
	 output [7:0] data_out,
	 input reset,
    input boot_enable,
	 output reg boot_done,
	 output reg cfg_done,
	 input phi,
	 input clk,
	 input cs,
	 input rw_in,
	 input [7:0] data_in,
	 input [2:0] addr_in
    );

wire	[23:0] flash_address;
reg	[23:0] flash_address_reg;
reg	[7:0] flash_reg;
reg	[7:0]	data_reg;
reg 	[7:0]	rcv_reg;
wire	[7:0]	flash_cmd;
reg	[7:0]	flash_cmd_reg;
wire	[7:0] flash_data;
reg 	[7:0]	flash_data_reg=8'hff;
reg	flash_active=1'b0;
reg	flash_ack=1'b0;
reg	phi_prev=1'b0;
reg	cs_prev=1'b0;
wire	flash_busy;
reg	[6:0] romtype=7'h0;
reg	[15:0] romlength=16'h0;
reg 	[15:0] countbytes=16'h0;
reg 	[3:0] cfgbyte=4'h0;
reg 	length_h=1'b0;
reg 	[1:0] rxcount=2'b0;


initial begin
boot_done=1'b0;
cfg_done=1'b0;
rw_out=1'b1;
addr_out=16'hffff;
end


parameter ROM_ADDR=24'h100000;
parameter CFG_ADDR=24'h0ff000;

reg [8:0] boot_state=IDLE;
localparam IDLE			=	9'b000000001;
localparam LOAD_TYPE		=	9'b000000010;
localparam LOAD_LENGTH	=	9'b000000100;
localparam LOAD_ROM		=	9'b000001000;
localparam LOAD_CFG		=	9'b000010000;
localparam WAIT_CMD		= 	9'b000100000;
localparam SEND			=	9'b001000000;
localparam RECEIVE		=	9'b010000000;
localparam CMD_ONLY		=	9'b100000000;	

localparam PP=8'h02;
localparam READ=8'h03;
localparam RDSR=8'h05;
localparam RDSCUR=8'h2b;
localparam RDID=8'h9f;


spiflash flash(
.clk(clk),
.din(data_reg),
.dout(flash_data),
.Addr(flash_address),
.cmd(flash_cmd),
.active(flash_active),
.ack(flash_ack),
.reset(reset),
.busy(flash_busy),
.flash_ck(flash_ck),
.flash_cs(flash_cs),
.flash_si(flash_si),
.flash_so(flash_so)
);

always @(posedge clk) begin
	phi_prev<=phi;
	cs_prev<=cs;
end

always @(posedge clk)
begin
flash_ack<=1'b0;
if(reset|~boot_enable)
	begin
	boot_state<=IDLE;
	flash_active<=1'b0;
	end
else if(phi_prev & ~phi)
	begin
		case(boot_state)
			IDLE:
				begin
				if(~boot_done)
					begin
					boot_state<=LOAD_TYPE;
					flash_active<=1'b1;
					flash_ack<=1'b1;
					end
				else
					begin
					boot_state<=WAIT_CMD;
					end
				end
		LOAD_TYPE:
				begin
				if(~flash_busy)
					begin
					rw_out<=1'b1;
					if(flash_data[7])
						begin
						boot_state<=LOAD_CFG;
						boot_done<=1'b1;
						flash_active<=1'b0;
						flash_ack<=1'b1;
						cfgbyte<=4'hf;
						end
					else
						begin
						romtype<=flash_data[6:0];
						flash_ack<=1'b1;
						boot_state<=LOAD_LENGTH;
						length_h<=1'b0;
						end
					end
				end
		LOAD_LENGTH:
				begin
				if(~flash_busy)
					begin
					if(~length_h)
						begin
						romlength[7:0]<=flash_data;
						flash_ack<=1'b1;
						length_h<=1'b1;
						end
					else 
						begin
						romlength[15:8]<=flash_data;
						flash_ack<=1'b1;
						boot_state<=LOAD_ROM;
						countbytes<=16'h0;
						end
					end
				end						
		LOAD_ROM:
				begin
				if(~flash_busy)
					begin
					rw_out<=1'b0;
					flash_data_reg<=flash_data;
					addr_out<=countbytes;
					if(countbytes==romlength)
						begin
						flash_ack<=1'b1;
						boot_state<=LOAD_TYPE;
						end
					else
						begin
						flash_ack<=1'b1;
						countbytes<=countbytes+16'b1;
						end
					end
				end
		LOAD_CFG:
				begin
				addr_out[15:0]<={12'hFD9,cfgbyte};
				if(~flash_busy)
					begin
					if(cfgbyte==4'hf)
						begin
						flash_active<=1'b1;
						end
					else if(cfgbyte==4'h6)
						begin
						rw_out<=1'b1;
						boot_state<=IDLE;
						flash_active<=1'b0;
						flash_ack<=1'b1;
						cfg_done<=1'b1;
						end
					else
						begin
						rw_out<=1'b0;
						flash_data_reg<=flash_data;							
						end
					flash_ack<=1'b1;
					cfgbyte<=cfgbyte+4'b1;
					end
				end
		WAIT_CMD:
				begin
				flash_data_reg<=8'hff;
				if(cs_prev & ~rw_in & addr_in==3'h0)
					begin
					flash_active<=1'b1;
					flash_ack<=1'b1;
					if(data_in==PP)
						begin
						boot_state<=SEND;	
						end
					else if(data_in==RDSR || data_in==RDSCUR || data_in==RDID || data_in==READ)
						begin
						boot_state<=RECEIVE;
						rxcount<=2'b0;
						end
					else
						begin
						boot_state<=CMD_ONLY;				
						end
					end
				end
		SEND:
				begin
				if(cs_prev & ~rw_in & ~flash_busy)
					begin
					case(addr_in)
						3'h0:	begin
								boot_state<=WAIT_CMD;
								flash_active<=1'b0;
								flash_ack<=1'b1;
								end
						3'h4:	begin
								flash_ack<=1;
								end
					endcase	
					end
				end
		RECEIVE:
				begin
				if (~flash_busy)
					begin
					rcv_reg<=flash_data;
					if ((flash_cmd_reg==RDID && rxcount==2'd2) || (cs_prev & ~rw_in & addr_in==3'h0) || flash_cmd_reg==RDSCUR || flash_cmd_reg==RDSR )
						begin
						boot_state<=WAIT_CMD;
						flash_active<=1'b0;
						flash_ack<=1'b1;
						end
					else if(cs_prev & rw_in & addr_in==3'h4)
						begin
						flash_ack<=1'b1;
						rxcount<=rxcount+2'b1;
						end
					end		
				end
		CMD_ONLY:
				begin
				if (~flash_busy)
					begin
					boot_state<=WAIT_CMD;
					flash_active<=1'b0;
					flash_ack<=1'b0;
					end		
				end
		endcase
	end
end



always @(posedge clk)
	begin
	if (cs_prev & ~cs & ~rw_in)
		begin
		case(addr_in)
			3'h0:	begin
					flash_cmd_reg<=data_in;
					end
			3'h1:	begin
					flash_address_reg[23:16]<=data_in;
					end
			3'h2:	begin
					flash_address_reg[15:8]<=data_in;
					end
			3'h3:	begin
					flash_address_reg[7:0]<=data_in;
					end
			3'h4: begin
					data_reg<=data_in;
					end
		endcase		
		end
	end
	
always @*
	begin
	flash_reg=8'hff;
	if(cs_prev & rw_in)
		begin
		case(addr_in)
			3'h0:	begin												
					flash_reg={6'b0,flash_active,flash_busy};
					end
			3'h1:	begin
					flash_reg=flash_address[23:16];
					end
			3'h2:	begin
					flash_reg=flash_address[15:8];
					end
			3'h3:	begin
					flash_reg=flash_address[7:0];
					end
			3'h4:	begin
					flash_reg=rcv_reg;
					end
		endcase
		end
	end


assign cs0=romtype[6];
assign cs1=~romtype[6];
assign addr_ext=romtype[5:0];
assign flash_address=(boot_state==LOAD_TYPE)?ROM_ADDR:
							(boot_state==LOAD_CFG)?CFG_ADDR:
							flash_address_reg;
assign flash_cmd=(cfg_done)?flash_cmd_reg:READ;
assign data_out=flash_reg&flash_data_reg;
endmodule
module bootstrap(
	 
	 output wire flash_cs,
	 output wire flash_ck,
	 output wire flash_si,
	 input wire flash_so,
 	 output cs0,
	 output cs1,
	 output reg rw_out,
	 output reg [15:0] addr_out,
	 output [5:0] addr_ext,
	 output [7:0] data_out,
	 input reset,
    input boot_enable,
	 output reg boot_done,
	 output reg cfg_done,
	 input phi,
	 input clk,
	 input cs,
	 input rw_in,
	 input [7:0] data_in,
	 input [2:0] addr_in
    );


wire	[23:0] flash_address;
reg	[23:0] flash_address_reg;
reg	[7:0] flash_reg;
reg	[7:0]	data_reg;
reg 	[7:0]	rcv_reg;
wire	[7:0]	flash_cmd;
reg	[7:0]	flash_cmd_reg;
wire	[7:0] flash_data;
reg 	[7:0]	flash_data_reg=8'hff;
reg	flash_active=1'b0;
reg	flash_ack=1'b0;
reg	phi_prev=1'b0;
reg	cs_prev=1'b0;
wire	flash_busy;
reg	[6:0] romtype=7'h0;
reg	[15:0] romlength=16'h0;
reg 	[15:0] countbytes=16'h0;
reg 	[3:0] cfgbyte=4'h0;
reg 	length_h=1'b0;
reg 	[1:0] rxcount=2'b0;


initial begin
boot_done=1'b0;
cfg_done=1'b0;
rw_out=1'b1;
addr_out=16'hffff;
end


parameter ROM_ADDR=24'h100000;
parameter CFG_ADDR=24'h0ff000;

reg [8:0] boot_state=IDLE;
localparam IDLE			=	9'b000000001;
localparam LOAD_TYPE		=	9'b000000010;
localparam LOAD_LENGTH	=	9'b000000100;
localparam LOAD_ROM		=	9'b000001000;
localparam LOAD_CFG		=	9'b000010000;
localparam WAIT_CMD		= 	9'b000100000;
localparam SEND			=	9'b001000000;
localparam RECEIVE		=	9'b010000000;
localparam CMD_ONLY		=	9'b100000000;	

localparam PP=8'h02;
localparam READ=8'h03;
localparam RDSR=8'h05;
localparam RDSCUR=8'h2b;
localparam RDID=8'h9f;


spiflash flash(
.clk(clk),
.din(data_reg),
.dout(flash_data),
.Addr(flash_address),
.cmd(flash_cmd),
.active(flash_active),
.ack(flash_ack),
.reset(reset),
.busy(flash_busy),
.flash_ck(flash_ck),
.flash_cs(flash_cs),
.flash_si(flash_si),
.flash_so(flash_so)
);

always @(posedge clk) begin
	phi_prev<=phi;
	cs_prev<=cs;
end

always @(posedge clk)
begin
flash_ack<=1'b0;
if(reset|~boot_enable)
	begin
	boot_state<=IDLE;
	flash_active<=1'b0;
	end
else if(phi_prev & ~phi)
	begin
		case(boot_state)
			IDLE:
				begin
				if(~boot_done)
					begin
					boot_state<=LOAD_TYPE;
					flash_active<=1'b1;
					flash_ack<=1'b1;
					end
				else
					begin
					boot_state<=WAIT_CMD;
					end
				end
		LOAD_TYPE:
				begin
				if(~flash_busy)
					begin
					rw_out<=1'b1;
					if(flash_data[7])
						begin
						boot_state<=LOAD_CFG;
						boot_done<=1'b1;
						flash_active<=1'b0;
						flash_ack<=1'b1;
						cfgbyte<=4'hf;
						end
					else
						begin
						romtype<=flash_data[6:0];
						flash_ack<=1'b1;
						boot_state<=LOAD_LENGTH;
						length_h<=1'b0;
						end
					end
				end
		LOAD_LENGTH:
				begin
				if(~flash_busy)
					begin
					if(~length_h)
						begin
						romlength[7:0]<=flash_data;
						flash_ack<=1'b1;
						length_h<=1'b1;
						end
					else 
						begin
						romlength[15:8]<=flash_data;
						flash_ack<=1'b1;
						boot_state<=LOAD_ROM;
						countbytes<=16'h0;
						end
					end
				end						
		LOAD_ROM:
				begin
				if(~flash_busy)
					begin
					rw_out<=1'b0;
					flash_data_reg<=flash_data;
					addr_out<=countbytes;
					if(countbytes==romlength)
						begin
						flash_ack<=1'b1;
						boot_state<=LOAD_TYPE;
						end
					else
						begin
						flash_ack<=1'b1;
						countbytes<=countbytes+16'b1;
						end
					end
				end
		LOAD_CFG:
				begin
				addr_out[15:0]<={12'hFD9,cfgbyte};
				if(~flash_busy)
					begin
					if(cfgbyte==4'hf)
						begin
						flash_active<=1'b1;
						end
					else if(cfgbyte==4'h6)
						begin
						rw_out<=1'b1;
						boot_state<=IDLE;
						flash_active<=1'b0;
						flash_ack<=1'b1;
						cfg_done<=1'b1;
						end
					else
						begin
						rw_out<=1'b0;
						flash_data_reg<=flash_data;							
						end
					flash_ack<=1'b1;
					cfgbyte<=cfgbyte+4'b1;
					end
				end
		WAIT_CMD:
				begin
				flash_data_reg<=8'hff;
				if(cs_prev & ~rw_in & addr_in==3'h0)
					begin
					flash_active<=1'b1;
					flash_ack<=1'b1;
					if(data_in==PP)
						begin
						boot_state<=SEND;	
						end
					else if(data_in==RDSR || data_in==RDSCUR || data_in==RDID || data_in==READ)
						begin
						boot_state<=RECEIVE;
						rxcount<=2'b0;
						end
					else
						begin
						boot_state<=CMD_ONLY;				
						end
					end
				end
		SEND:
				begin
				if(cs_prev & ~rw_in & ~flash_busy)
					begin
					case(addr_in)
						3'h0:	begin
								boot_state<=WAIT_CMD;
								flash_active<=1'b0;
								flash_ack<=1'b1;
								end
						3'h4:	begin
								flash_ack<=1;
								end
					endcase	
					end
				end
		RECEIVE:
				begin
				if (~flash_busy)
					begin
					rcv_reg<=flash_data;
					if ((flash_cmd_reg==RDID && rxcount==2'd2) || (cs_prev & ~rw_in & addr_in==3'h0) || flash_cmd_reg==RDSCUR || flash_cmd_reg==RDSR )
						begin
						boot_state<=WAIT_CMD;
						flash_active<=1'b0;
						flash_ack<=1'b1;
						end
					else if(cs_prev & rw_in & addr_in==3'h4)
						begin
						flash_ack<=1'b1;
						rxcount<=rxcount+2'b1;
						end
					end		
				end
		CMD_ONLY:
				begin
				if (~flash_busy)
					begin
					boot_state<=WAIT_CMD;
					flash_active<=1'b0;
					flash_ack<=1'b0;
					end		
				end
		endcase
	end
end



always @(posedge clk)
	begin
	if (cs_prev & ~cs & ~rw_in)
		begin
		case(addr_in)
			3'h0:	begin
					flash_cmd_reg<=data_in;
					end
			3'h1:	begin
					flash_address_reg[23:16]<=data_in;
					end
			3'h2:	begin
					flash_address_reg[15:8]<=data_in;
					end
			3'h3:	begin
					flash_address_reg[7:0]<=data_in;
					end
			3'h4: begin
					data_reg<=data_in;
					end
		endcase		
		end
	end
	
always @*
	begin
	flash_reg=8'hff;
	if(cs_prev & rw_in)
		begin
		case(addr_in)
			3'h0:	begin												
					flash_reg={6'b0,flash_active,flash_busy};
					end
			3'h1:	begin
					flash_reg=flash_address[23:16];
					end
			3'h2:	begin
					flash_reg=flash_address[15:8];
					end
			3'h3:	begin
					flash_reg=flash_address[7:0];
					end
			3'h4:	begin
					flash_reg=rcv_reg;
					end
		endcase
		end
	end


assign cs0=romtype[6];
assign cs1=~romtype[6];
assign addr_ext=romtype[5:0];
assign flash_address=(boot_state==LOAD_TYPE)?ROM_ADDR:
							(boot_state==LOAD_CFG)?CFG_ADDR:
							flash_address_reg;
assign flash_cmd=(cfg_done)?flash_cmd_reg:READ;
assign data_out=flash_reg&flash_data_reg;
endmodule