target_pipelined_wb.lua 10.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
-- -*- Mode: LUA; tab-width: 2 -*-


MAX_ACK_LENGTH = 10;

MODE_CLASSIC=1
MODE_PIPELINED=2

function gen_pipelined_wb_ports(mode)
  local ports = {
11 12
		port(BIT, 0, "in", "rst_n_i", "", 0),
		port(BIT, 0, "in", "clk_sys_i", "", 0),
13 14 15 16 17 18 19 20 21 22 23 24 25
	};
	
  if(address_bus_width > 0 ) then
		table_join(ports, { port(SLV, address_bus_width, "in", "wb_adr_i", "", VPORT_WB) })
  end

  table_join(ports, {
										port(SLV, DATA_BUS_WIDTH, "in", "wb_dat_i", "", VPORT_WB),
										port(SLV, DATA_BUS_WIDTH, "out", "wb_dat_o", "", VPORT_WB),
										port(BIT, 0, "in", "wb_cyc_i", "", VPORT_WB),
										port(SLV, math.floor((DATA_BUS_WIDTH+7)/8), "in", "wb_sel_i", "", VPORT_WB),
										port(BIT, 0, "in", "wb_stb_i", "", VPORT_WB),
										port(BIT, 0, "in", "wb_we_i", "", VPORT_WB),
26 27 28
										port(BIT, 0, "out", "wb_ack_o", "", VPORT_WB),
										port(BIT, 0, "out", "wb_err_o", "", VPORT_WB),
										port(BIT, 0, "out", "wb_rty_o", "", VPORT_WB)
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
   									});

	if(mode == MODE_PIPELINED) then
		table_join(ports, {	port(BIT, 0, "out", "wb_stall_o", "", VPORT_WB) });
	end

	if(periph.irqcount > 0) then
		table_join(ports, { port(BIT, 0, "out" ,"wb_int_o", "", VPORT_WB); });
	end

    
	add_global_ports(ports);
end


function gen_pipelined_wb_signals(mode)
local width = math.max(1, address_bus_width);

local wb_sigs = {  signal(SLV, MAX_ACK_LENGTH, "ack_sreg"),
									 signal(SLV, DATA_BUS_WIDTH, "rddata_reg"),
									 signal(SLV, DATA_BUS_WIDTH, "wrdata_reg"),
50
 									 signal(SLV, DATA_BUS_WIDTH/8	, "bwsel_reg"),
51
									 signal(SLV, width, "rwaddr_reg"),
52 53 54 55 56 57
									 signal(BIT, 0, "ack_in_progress"),
 									 signal(BIT, 0, "wr_int"),
 									 signal(BIT, 0, "rd_int"),
									 signal(SLV, DATA_BUS_WIDTH, "allones"),
							 		 signal(SLV, DATA_BUS_WIDTH, "allzeros")
 				
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
							 };

	add_global_signals(wb_sigs);
end


-- generates the entire Wishbone bus access logic
function gen_bus_logic_pipelined_wb(mode)
  local s;

	gen_pipelined_wb_ports(mode);
	gen_pipelined_wb_signals(mode);

  foreach_reg(ALL_REG_TYPES, function(reg) 
			gen_abstract_code(reg);
    end );

	local resetcode={};
	local ackgencode={};
	local preackcode={};
	
  foreach_field(function(field, reg) 
    table_join(resetcode, field.reset_code_main); 
  end );

  foreach_reg(ALL_REG_TYPES, function(reg) 
    table_join(resetcode, reg.reset_code_main); 
  end );


  foreach_reg({TYPE_REG}, function(reg) 
	    						foreach_subfield(reg, function(field, reg) 
							     table_join(ackgencode, field.ackgen_code); 
							     table_join(preackcode, field.ackgen_code_pre); 
								end);
							 table_join(ackgencode, reg.ackgen_code); 
					     table_join(preackcode, reg.ackgen_code_pre); 
					    end);

	local fsmcode={};

  foreach_reg({TYPE_REG}, function(reg) 
    local acklen = find_max(reg, "acklen");
		local rcode={};
		local wcode={};
	
					
		foreach_subfield(reg, function(field, reg) table_join(wcode, field.write_code); end );
		foreach_subfield(reg, function(field, reg) table_join(rcode, field.read_code); end );		

108
		local padcode = fill_unused_bits("rddata_reg", reg, options.unused_zeroes);
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183


		table_join(wcode, reg.write_code);
		table_join(rcode, reg.read_code);

		local rwcode = {
			vif(vequal("wb_we_i" ,1), {
	   		wcode,
			});
				rcode,
	   		padcode
			}; 


	 if(not (reg.dont_emit_ack_code == true))  then -- we don't want the main generator to mess with ACK line for this register
			table_join(rwcode, { va(vi("ack_sreg", math.max(acklen-1, 0)), 1); } );
			table_join(rwcode, { va("ack_in_progress", 1); });
	 end

   	if(regbank_address_bits > 0) then
   		rwcode = { vcase(reg.base, rwcode); }; 
   	end
		
		table_join(fsmcode, rwcode);
	end );

  if(regbank_address_bits > 0) then

		table_join(fsmcode, { vcasedefault({
			vcomment("prevent the slave from hanging the bus on invalid address");
			va("ack_in_progress", 1);
			va(vi("ack_sreg", 0), 1);
		}); });

   	fsmcode = { vswitch(vi("rwaddr_reg", regbank_address_bits - 1, 0), fsmcode); };
  end
  
  if(periph.ramcount > 0) then
		local ramswitchcode = {};
		
		if(periph.fifocount + periph.regcount > 0) then
-- append the register bank CASE statement if there are any registers or FIFOs
			ramswitchcode = { vcase (0, fsmcode); };
		end
		
		
		
	
		foreach_reg({TYPE_RAM}, function(reg) 

								local acklen = csel(options.register_data_output, 1, 0);												
								table_join(ramswitchcode, { vcase(reg.select_bits , {
									vif(vequal("rd_int" ,1), {
							 			va(vi("ack_sreg", 0), 1);
									}, {
							 			va(vi("ack_sreg", acklen), 1);
									});
									va("ack_in_progress", 1);
									} ); } );
							end);

		table_join(ramswitchcode, { 
			vcasedefault({
				vcomment("prevent the slave from hanging the bus on invalid address");
				va("ack_in_progress", 1);
				va(vi("ack_sreg", 0), 1);
			 }) 
		});

  	fsmcode = { vswitch(vi("rwaddr_reg", address_bus_width-1, address_bus_width - address_bus_select_bits), ramswitchcode); };
  end

	fsmcode = { vif(vand(vequal("wb_cyc_i", 1), vequal("wb_stb_i", 1)), { fsmcode } ); };	

		local code = {
184
		vcomment("Some internal signals assignments");
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
		va("wrdata_reg", "wb_dat_i");
		
		vcomment("");
		vcomment("Main register bank access process.");

		vsyncprocess("clk_sys_i", "rst_n_i", {
		 vreset(0, {
			va("ack_sreg", 0);
			va("ack_in_progress", 0);
			va("rddata_reg", 0);
			resetcode
		 });

		 vposedge ({
			 vcomment("advance the ACK generator shift register");
			 va(vi("ack_sreg", MAX_ACK_LENGTH-2, 0), vi("ack_sreg", MAX_ACK_LENGTH-1, 1));
			 va(vi("ack_sreg", MAX_ACK_LENGTH-1), 0);
		
			 vif(vequal("ack_in_progress", 1), {
			 	 vif(vequal(vi("ack_sreg", 0), 1), { ackgencode; va("ack_in_progress", 0); }, preackcode);
			 }, { 
				fsmcode
			 });
			});
		});
	};		 


-- we have some RAMs in our slave?
	if(periph.ramcount > 0) then

216 217 218 219 220 221 222 223 224 225 226
		 local ram_signals_code = {
				vcomment("Some internal signals assignments used by RAMs.");
				va("bwsel_reg", "wb_sel_i");
				va("rd_int", vand("wb_cyc_i", vand("wb_stb_i", vnot("wb_we_i"))));
				va("wr_int", vand("wb_cyc_i", vand("wb_stb_i", "wb_we_i")));
				va("allones", vothers(1));
				va("allzeros", vothers(0));
		 };

		 table_join(code, ram_signals_code);

227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
-- the data output is muxed between RAMs and register bank. Here we generate a combinatorial mux if we don't want the output to be registered. This gives us
-- memory access time of 2 clock cycles. Otherwise the ram output is handled by the main process.
		if(not options.register_data_output) then
			local sens_list = {"rddata_reg","rwaddr_reg"};
			local mux_switch_code = {};
			local mux_code = {vswitch(vi("rwaddr_reg", address_bus_width-1, address_bus_width - address_bus_select_bits), mux_switch_code); };

			local output_mux_process = {vcomment("Data output multiplexer process"); vcombprocess(sens_list, mux_code);};
			
			 foreach_reg({TYPE_RAM}, function(reg) 
  								table.insert(sens_list, reg.full_prefix.."_rddata_int");
									
									local assign_code =  { va(vi("wb_dat_o", reg.width-1, 0), reg.full_prefix.."_rddata_int"); };
									
									if(reg.width < DATA_BUS_WIDTH)  then
										table_join(assign_code, { va(vi("wb_dat_o", DATA_BUS_WIDTH-1, reg.width), 0); });
									end
									
  								

  								table_join(mux_switch_code, { vcase(reg.select_bits, assign_code ); } );
  						end);

			table.insert(sens_list, "wb_adr_i");		

			table_join(mux_switch_code, {vcasedefault(va("wb_dat_o", "rddata_reg")); } );
			table_join(code, output_mux_process);
		end

-- now generate an address decoder for the RAMs, driving rd_i and wr_i lines.

		local sens_list = { "wb_adr_i", "rd_int", "wr_int" };
		local proc_body = { };

			 foreach_reg({TYPE_RAM}, function(reg) 
  							--	table.insert(sens_list, reg.full_prefix.."_rddata_int");
									table_join(proc_body, {vif(vequal(vi("wb_adr_i", address_bus_width-1, address_bus_width - address_bus_select_bits), reg.select_bits), {
										va(reg.full_prefix.."_rd_int", "rd_int");
										va(reg.full_prefix.."_wr_int", "wr_int");
									}, {
										va(reg.full_prefix.."_wr_int", 0);
										va(reg.full_prefix.."_rd_int", 0);
									});  });
  						end);

		table_join(code, {vcomment("Read & write lines decoder for RAMs"); vcombprocess(sens_list, proc_body); });

	else -- no RAMs in design? wire rddata_reg directly to wb_data_o
		table_join(code, {vcomment("Drive the data output bus"); va("wb_dat_o", "rddata_reg") } );
	end

278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
  
  foreach_reg(ALL_REG_TYPES, 
              function(reg) 
                 ex_code = {}

                 if(reg.extra_code ~= nil) then
                    table_join(ex_code, {vcomment("extra code for reg/fifo/mem: "..reg.name);});
                    table_join(ex_code, reg.extra_code);

                 end

                 foreach_subfield(reg, 
                                  function(field, reg) 
                                     if (field.extra_code ~= nil) then
                                        table_join(ex_code, {vcomment(field.name); field.extra_code}); 
                                     end	
                                  end );
                 
                 if(reg.optional == nil) then
                    table_join(code, ex_code)
                 else
299 300 301 302 303 304 305 306 307 308 309
                    table_join(code, {vgenerate_if(vnot(vequal(reg.optional, 0)), ex_code )} );
										-- also create complementary if-generate to drive unused outputs when condition is false
										gen_ex_code = {}
										for i, v in ipairs(reg.ports) do
											 if v.dir == "out" then
													table_join(gen_ex_code, {va(v.name, 0);});
											 end
										end
										if gen_ex_code ~= nil then
											 table_join(code, {vgenerate_if(vequal(reg.optional, 0), gen_ex_code )} );
										end
310
                 end
311 312

							end);
313 314


315 316
	
	if(address_bus_width > 0) then
317 318 319 320 321 322
      if(options.hdl_reg_style == "record_full") then
		table_join(code, { 	va("rwaddr_reg", vi("wb_adr_i", address_bus_width + 2 - 1, 2) );	} );
      else
		table_join(code, { 	va("rwaddr_reg", "wb_adr_i" );	} );
      end
        
323 324 325 326 327
	else
		table_join(code, { 	va("rwaddr_reg", vothers(0));	});
	end

	table_join(code, { va("wb_stall_o", vand(vnot(vi("ack_sreg", 0)), vand("wb_stb_i","wb_cyc_i")))});
328 329
	table_join(code, { va("wb_err_o", 0 ) } );
	table_join(code, { va("wb_rty_o", 0 ) } );
330 331 332 333 334 335 336 337
	
	table_join(code, { vcomment("ACK signal generation. Just pass the LSB of ACK counter.");
										 va("wb_ack_o", vi("ack_sreg", 0));
										});

	return code;
end