Mercurial > pygar
view modules/bluespec/Pygar/core/Processor.bsv @ 49:61f6267cb3db pygar svn.50
[svn r50] removed problematic stats stuff
author | rlm |
---|---|
date | Wed, 05 May 2010 14:40:48 -0400 |
parents | 4d87fa55a776 |
children | 2b18894f75e2 |
line wrap: on
line source
1 /// The MIT License3 // Copyright (c) 2009 Massachusetts Institute of Technology5 // Permission is hereby granted, free of charge, to any person obtaining a copy6 // of this software and associated documentation files (the "Software"), to deal7 // in the Software without restriction, including without limitation the rights8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell9 // copies of the Software, and to permit persons to whom the Software is10 // furnished to do so, subject to the following conditions:12 // The above copyright notice and this permission notice shall be included in13 // all copies or substantial portions of the Software.15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN21 // THE SOFTWARE.24 import Connectable::*;25 import GetPut::*;26 import ClientServer::*;27 import RegFile::*;29 import FIFO::*;30 import FIFOF::*;31 import SFIFO::*;32 import RWire::*;34 import Trace::*;35 import BFIFO::*;36 import MemTypes::*;37 import ProcTypes::*;38 import BRegFile::*;39 import BranchPred::*;40 //import PathTypes::*; This is only there to force the debugging42 //AWB includes43 `include "asim/provides/low_level_platform_interface.bsh"44 `include "asim/provides/soft_connections.bsh"45 `include "asim/provides/common_services.bsh"47 // Local includes48 //`include "asim/provides/processor_library.bsh" (included above directly)50 `include "asim/provides/common_services.bsh"51 `include "asim/dict/STATS_PROCESSOR.bsh"52 `include "asim/provides/processor_library.bsh"54 // Local includes. Look for the correspondingly named .awb files55 // workspace/labs/src/mit-6.375/modules/bluespec/mit-6.375/common/56 // to find the actual Bluespec files which are used to generate57 // these includes. These files are specific to this audio processing58 // pipeline60 `include "asim/provides/audio_pipe_types.bsh"62 //interface CPUToHost;63 // method Bit#(32) cpuToHost(int req);64 //endinterface66 interface Proc;68 // Interface from processor to caches69 interface Client#(DataReq,DataResp) dmem_client;70 interface Client#(InstReq,InstResp) imem_client;72 // Interface for enabling/disabling statistics on the rest of the core73 interface Get#(Bool) statsEn_get;75 // // Interface to host76 // interface CPUToHost tohost;78 // Interface to Audio Pipeline79 interface Get#(AudioProcessorUnit) sampleOutput;80 interface Put#(AudioProcessorUnit) sampleInput;82 endinterface84 typedef enum { PCgen, Exec, Writeback } Stage deriving(Eq,Bits);86 //-----------------------------------------------------------87 // Register file module88 //-----------------------------------------------------------90 interface BRFile;91 method Action wr( Rindx rindx, Bit#(32) data );92 method Bit#(32) rd1( Rindx rindx );93 method Bit#(32) rd2( Rindx rindx );94 endinterface96 module mkBRFile( BRFile );98 RegFile#(Rindx,Bit#(32)) rfile <- mkBRegFile();100 method Action wr( Rindx rindx, Bit#(32) data );101 rfile.upd( rindx, data );102 endmethod104 method Bit#(32) rd1( Rindx rindx );105 return ( rindx == 0 ) ? 0 : rfile.sub(rindx);106 endmethod108 method Bit#(32) rd2( Rindx rindx );109 return ( rindx == 0 ) ? 0 : rfile.sub(rindx);110 endmethod112 endmodule114 //-----------------------------------------------------------115 // Helper functions116 //-----------------------------------------------------------118 function Bit#(32) slt( Bit#(32) val1, Bit#(32) val2 );119 return zeroExtend( pack( signedLT(val1,val2) ) );120 endfunction122 function Bit#(32) sltu( Bit#(32) val1, Bit#(32) val2 );123 return zeroExtend( pack( val1 < val2 ) );124 endfunction126 function Bit#(32) rshft( Bit#(32) val );127 return zeroExtend(val[4:0]);128 endfunction131 //-----------------------------------------------------------132 // Find funct for wbQ133 //-----------------------------------------------------------134 function Bool findwbf(Rindx fVal, WBResult cmpVal);135 case (cmpVal) matches136 tagged WB_ALU {data:.res, dest:.rd} :137 return (fVal == rd);138 tagged WB_Load .rd :139 return (fVal == rd);140 tagged WB_Store .st :141 return False;142 tagged WB_Host .x :143 return False;144 endcase145 endfunction148 //-----------------------------------------------------------149 // Stall funct for wbQ150 //-----------------------------------------------------------151 function Bool stall(Instr inst, SFIFO#(WBResult, Rindx) f);152 case (inst) matches153 // -- Memory Ops ------------------------------------------------154 tagged LW .it :155 return f.find(it.rbase);156 tagged SW {rsrc:.dreg, rbase:.addr, offset:.o} :157 return (f.find(addr) || f.find2(dreg));159 // -- Simple Ops ------------------------------------------------160 tagged ADDIU .it : return f.find(it.rsrc);161 tagged SLTI .it : return f.find(it.rsrc);162 tagged SLTIU .it : return f.find(it.rsrc);163 tagged ANDI .it : return f.find(it.rsrc);164 tagged ORI .it : return f.find(it.rsrc);165 tagged XORI .it : return f.find(it.rsrc);167 tagged LUI .it : return f.find(it.rdst); //this rds/wrs itself168 tagged SLL .it : return f.find(it.rsrc);169 tagged SRL .it : return f.find(it.rsrc);170 tagged SRA .it : return f.find(it.rsrc);171 tagged SLLV .it : return (f.find(it.rsrc) || f.find(it.rshamt));172 tagged SRLV .it : return (f.find(it.rsrc) || f.find(it.rshamt));173 tagged SRAV .it : return (f.find(it.rsrc) || f.find(it.rshamt));174 tagged ADDU .it : return (f.find(it.rsrc1) || f.find2(it.rsrc2));175 tagged SUBU .it : return (f.find(it.rsrc1) || f.find2(it.rsrc2));176 tagged AND .it : return (f.find(it.rsrc1) || f.find2(it.rsrc2));177 tagged OR .it : return (f.find(it.rsrc1) || f.find2(it.rsrc2));178 tagged XOR .it : return (f.find(it.rsrc1) || f.find2(it.rsrc2));179 tagged NOR .it : return (f.find(it.rsrc1) || f.find2(it.rsrc2));180 tagged SLT .it : return (f.find(it.rsrc1) || f.find2(it.rsrc2));181 tagged SLTU .it : return (f.find(it.rsrc1) || f.find2(it.rsrc2));184 // -- Branches --------------------------------------------------186 tagged BLEZ .it : return (f.find(it.rsrc));187 tagged BGTZ .it : return (f.find(it.rsrc));188 tagged BLTZ .it : return (f.find(it.rsrc));189 tagged BGEZ .it : return (f.find(it.rsrc));190 tagged BEQ .it : return (f.find(it.rsrc1) || f.find2(it.rsrc2));191 tagged BNE .it : return (f.find(it.rsrc1) || f.find2(it.rsrc2));193 // -- Jumps -----------------------------------------------------195 tagged J .it : return False;196 tagged JR .it : return f.find(it.rsrc);197 tagged JALR .it : return f.find(it.rsrc);198 tagged JAL .it : return False;200 // -- Cop0 ------------------------------------------------------202 tagged MTC0 .it : return f.find(it.rsrc);203 tagged MFC0 .it : return False;205 // -- Illegal ---------------------------------------------------207 default : return False;209 endcase210 endfunction211 //-----------------------------------------------------------212 // Reference processor213 //-----------------------------------------------------------216 //(* doc = "synthesis attribute ram_style mkProc distributed;" *)217 //(* synthesize *)219 module [CONNECTED_MODULE] mkProc( Proc );221 //-----------------------------------------------------------222 // State224 // Standard processor state226 Reg#(Addr) pc <- mkReg(32'h00001000);227 Reg#(Epoch) epoch <- mkReg(0);228 Reg#(Stage) stage <- mkReg(PCgen);229 BRFile rf <- mkBRFile;231 // Branch Prediction232 BranchPred bp <- mkBranchPred();233 FIFO#(PCStat) execpc <- mkLFIFO();235 // Pipelines236 FIFO#(PCStat) pcQ <-mkSizedFIFO(3);237 SFIFO#(WBResult, Rindx) wbQ <-mkSFIFO(findwbf);239 // NEED TO ADD CAPABILITY FOR RESET (should be able to just say if I get valid in and these are flagged, clear them.240 Reg#(Bit#(32)) cp0_tohost <- mkReg(0);241 Reg#(Bit#(32)) cp0_fromhost <- mkReg(0);242 Reg#(Bool) cp0_statsEn <- mkReg(False);243 Reg#(Bool) cp0_audioEOF <- mkReg(False); // Register to let code that EOF is reached244 Reg#(Bool) cp0_progComp <- mkReg(False); // Register to let processor know that the program is complete (as this terminates)246 // Memory request/response state248 FIFO#(InstReq) instReqQ <- mkBFIFO1();249 FIFO#(InstResp) instRespQ <- mkFIFO();251 FIFO#(DataReq) dataReqQ <- mkBFIFO1();252 FIFO#(DataResp) dataRespQ <- mkFIFO();254 // Audio I/O255 FIFO#(AudioProcessorUnit) inAudioFifo <- mkSizedFIFO(512);256 FIFO#(AudioProcessorUnit) outAudioFifo <- mkFIFO;259 // Statistics state (2010)260 // Reg#(Stat) num_cycles <- mkReg(0);261 // Reg#(Stat) num_inst <- mkReg(0);263 //Or:264 // Statistics state266 //rlm: removing these to avoid their broken stupidness.267 //STAT num_cycles <- mkStatCounter(`STATS_PROCESSOR_CYCLE_COUNT);268 //STAT num_inst <- mkStatCounter(`STATS_PROCESSOR_INST_COUNT);270 //-----------------------------------------------------------271 // Rules273 (* descending_urgency = "exec, pcgen" *)274 rule pcgen; //( stage == PCgen );275 let pc_plus4 = pc + 4;277 traceTiny("mkProc", "pc",pc);278 traceTiny("mkProc", "pcgen","P");279 instReqQ.enq( LoadReq{ addr:pc, tag:epoch} );281 let next_pc = bp.get(pc);282 if (next_pc matches tagged Valid .npc)283 begin284 pcQ.enq(PCStat {qpc:pc, qnxtpc:npc, qepoch:epoch});285 pc <= npc;286 end287 else288 begin289 pcQ.enq(PCStat {qpc:pc, qnxtpc:pc_plus4, qepoch:epoch});290 pc <= pc_plus4;291 end293 endrule295 rule discard (instRespQ.first() matches tagged LoadResp .ld296 &&& ld.tag != epoch);297 traceTiny("mkProc", "stage", "D");298 instRespQ.deq();299 endrule301 (* conflict_free = "exec, writeback" *)302 rule exec (instRespQ.first() matches tagged LoadResp.ld303 &&& (ld.tag == epoch)304 &&& unpack(ld.data) matches .inst305 &&& !stall(inst, wbQ));307 // Some abbreviations308 let sext = signExtend;309 let zext = zeroExtend;310 let sra = signedShiftRight;312 // Get the instruction314 instRespQ.deq();315 Instr inst316 = case ( instRespQ.first() ) matches317 tagged LoadResp .ld : return unpack(ld.data);318 tagged StoreResp .st : return ?;319 endcase;321 // Get the PC info322 let instrpc = pcQ.first().qpc;323 let pc_plus4 = instrpc + 4;325 Bool branchTaken = False;326 Addr newPC = pc_plus4;328 // Tracing329 traceTiny("mkProc", "exec","X");330 traceTiny("mkProc", "exInstTiny",inst);331 traceFull("mkProc", "exInstFull",inst);333 case ( inst ) matches335 // -- Memory Ops ------------------------------------------------337 tagged LW .it :338 begin339 Addr addr = rf.rd1(it.rbase) + sext(it.offset);340 dataReqQ.enq( LoadReq{ addr:addr, tag:zeroExtend(it.rdst) } );341 wbQ.enq(tagged WB_Load it.rdst);342 end344 tagged SW .it :345 begin346 Addr addr = rf.rd1(it.rbase) + sext(it.offset);347 dataReqQ.enq( StoreReq{ tag:0, addr:addr, data:rf.rd2(it.rsrc) } );348 wbQ.enq(tagged WB_Store);349 end351 // -- Simple Ops ------------------------------------------------353 tagged ADDIU .it :354 begin355 Bit#(32) result = rf.rd1(it.rsrc) + sext(it.imm);356 wbQ.enq(tagged WB_ALU {data:result, dest:it.rdst});357 end358 tagged SLTI .it : wbQ.enq(tagged WB_ALU {dest:it.rdst, data:slt( rf.rd1(it.rsrc), sext(it.imm) )});359 tagged SLTIU .it : wbQ.enq(tagged WB_ALU {dest:it.rdst, data:sltu( rf.rd1(it.rsrc), sext(it.imm) ) });360 tagged ANDI .it :361 begin362 Bit#(32) zext_it_imm = zext(it.imm);363 wbQ.enq(tagged WB_ALU {dest:it.rdst, data:(rf.rd1(it.rsrc) & zext_it_imm)} );364 end365 tagged ORI .it :366 begin367 Bit#(32) zext_it_imm = zext(it.imm);368 wbQ.enq(tagged WB_ALU {dest:it.rdst, data:(rf.rd1(it.rsrc) | zext_it_imm)} );369 end370 tagged XORI .it :371 begin372 Bit#(32) zext_it_imm = zext(it.imm);373 wbQ.enq(tagged WB_ALU {dest: it.rdst, data:(rf.rd1(it.rsrc) ^ zext_it_imm )});374 end375 tagged LUI .it :376 begin377 Bit#(32) zext_it_imm = zext(it.imm);378 wbQ.enq(tagged WB_ALU {dest: it.rdst, data:(zext_it_imm << 32'd16) });379 end381 tagged SLL .it :382 begin383 Bit#(32) zext_it_shamt = zext(it.shamt);384 wbQ.enq(tagged WB_ALU {dest: it.rdst, data:(rf.rd1(it.rsrc) << zext_it_shamt )} );385 end386 tagged SRL .it :387 begin388 Bit#(32) zext_it_shamt = zext(it.shamt);389 wbQ.enq(tagged WB_ALU {dest: it.rdst, data:(rf.rd1(it.rsrc) >> zext_it_shamt )});390 end391 tagged SRA .it :392 begin393 Bit#(32) zext_it_shamt = zext(it.shamt);394 wbQ.enq(tagged WB_ALU {dest: it.rdst, data:sra( rf.rd1(it.rsrc), zext_it_shamt )});395 end396 tagged SLLV .it : wbQ.enq(tagged WB_ALU {dest: it.rdst, data:(rf.rd1(it.rsrc) << rshft(rf.rd2(it.rshamt)) )});397 tagged SRLV .it : wbQ.enq(tagged WB_ALU {dest: it.rdst, data:(rf.rd1(it.rsrc) >> rshft(rf.rd2(it.rshamt)) )} );398 tagged SRAV .it : wbQ.enq(tagged WB_ALU {dest: it.rdst, data:sra( rf.rd1(it.rsrc), rshft(rf.rd2(it.rshamt)) ) });399 tagged ADDU .it : wbQ.enq(tagged WB_ALU {dest: it.rdst, data:(rf.rd1(it.rsrc1) + rf.rd2(it.rsrc2) )} );400 tagged SUBU .it : wbQ.enq(tagged WB_ALU {dest: it.rdst, data:(rf.rd1(it.rsrc1) - rf.rd2(it.rsrc2) )} );401 tagged AND .it : wbQ.enq(tagged WB_ALU {dest: it.rdst, data:(rf.rd1(it.rsrc1) & rf.rd2(it.rsrc2) )} );402 tagged OR .it : wbQ.enq(tagged WB_ALU {dest: it.rdst, data:(rf.rd1(it.rsrc1) | rf.rd2(it.rsrc2) )} );403 tagged XOR .it : wbQ.enq(tagged WB_ALU {dest: it.rdst, data:(rf.rd1(it.rsrc1) ^ rf.rd2(it.rsrc2) )} );404 tagged NOR .it : wbQ.enq(tagged WB_ALU {dest: it.rdst, data:(~(rf.rd1(it.rsrc1) | rf.rd2(it.rsrc2)) )} );405 tagged SLT .it : wbQ.enq(tagged WB_ALU {dest: it.rdst, data:slt( rf.rd1(it.rsrc1), rf.rd2(it.rsrc2) ) });406 tagged SLTU .it : wbQ.enq(tagged WB_ALU {dest: it.rdst, data:sltu( rf.rd1(it.rsrc1), rf.rd2(it.rsrc2) ) });408 // -- Branches --------------------------------------------------410 tagged BLEZ .it :411 if ( signedLE( rf.rd1(it.rsrc), 0 ) )412 begin413 newPC = pc_plus4 + (sext(it.offset) << 2);414 branchTaken = True;415 end417 tagged BGTZ .it :418 if ( signedGT( rf.rd1(it.rsrc), 0 ) )419 begin420 newPC = pc_plus4 + (sext(it.offset) << 2);421 branchTaken = True;422 end424 tagged BLTZ .it :425 if ( signedLT( rf.rd1(it.rsrc), 0 ) )426 begin427 newPC = pc_plus4 + (sext(it.offset) << 2);428 branchTaken = True;429 end431 tagged BGEZ .it :432 if ( signedGE( rf.rd1(it.rsrc), 0 ) )433 begin434 newPC = pc_plus4 + (sext(it.offset) << 2);435 branchTaken = True;436 end438 tagged BEQ .it :439 if ( rf.rd1(it.rsrc1) == rf.rd2(it.rsrc2) )440 begin441 newPC = pc_plus4 + (sext(it.offset) << 2);442 branchTaken = True;443 end445 tagged BNE .it :446 if ( rf.rd1(it.rsrc1) != rf.rd2(it.rsrc2) )447 begin448 newPC = pc_plus4 + (sext(it.offset) << 2);449 branchTaken = True;450 end452 // -- Jumps -----------------------------------------------------454 tagged J .it :455 begin456 newPC = { pc_plus4[31:28], it.target, 2'b0 };457 branchTaken = True;458 end460 tagged JR .it :461 begin462 newPC = rf.rd1(it.rsrc);463 branchTaken = True;464 end466 tagged JAL .it :467 begin468 wbQ.enq(tagged WB_ALU {dest:31, data:pc_plus4 });469 newPC = { pc_plus4[31:28], it.target, 2'b0 };470 branchTaken = True;471 end473 tagged JALR .it :474 begin475 wbQ.enq(tagged WB_ALU {dest:it.rdst, data:pc_plus4 });476 newPC = rf.rd1(it.rsrc);477 branchTaken = True;478 end480 // -- Cop0 ------------------------------------------------------482 tagged MTC0 .it : //Recieve things from host computer483 begin484 // $display( " PROCESSOR MTC0 call\n");485 case ( it.cop0dst )486 5'd10 : cp0_statsEn <= unpack(truncate(rf.rd1(it.rsrc)));487 5'd21 : cp0_tohost <= truncate(rf.rd1(it.rsrc));488 5'd26 : cp0_progComp <= unpack(truncate(rf.rd1(it.rsrc))); //states audio program completed and termination okay489 5'd27 : outAudioFifo.enq(tagged Sample unpack(truncate(rf.rd1(it.rsrc)))); //Bit size is 16 not 32490 default :491 $display( " RTL-ERROR : %m : Illegal MTC0 cop0dst register!" );492 endcase493 wbQ.enq(tagged WB_Host 0); //no idea wwhat this actually should be.494 end496 //this is host stuff?497 tagged MFC0 .it : //Things out498 begin499 case ( it.cop0src )500 // not actually an ALU instruction but don't have the format otherwise501 5'd10 : wbQ.enq(tagged WB_ALU {dest:it.rdst, data:zext(pack(cp0_statsEn)) });502 5'd20 : wbQ.enq(tagged WB_ALU {dest:it.rdst, data:cp0_fromhost });503 5'd21 : wbQ.enq(tagged WB_ALU {dest:it.rdst, data:cp0_tohost });504 5'd25 : begin505 // $display( "**** EOF Requested\n ");506 wbQ.enq(tagged WB_ALU {dest:it.rdst, data:zext(pack(cp0_audioEOF)) }); // Reading clears bit507 cp0_audioEOF <= False;508 end509 5'd28 : begin510 // $display( "***** Reqesting Sample \n");511 let sample = inAudioFifo.first(); // is this going to cause perf. delay?512 if (sample matches tagged Sample .audio) // if it is EOF another rule sets the cp0_audioEOF513 wbQ.enq(tagged WB_ALU {dest:it.rdst, data:zext(pack(audio)) }); // do I need pack?514 else $display ( "Audio File EOF Reached. Invalid sample request.");515 inAudioFifo.deq();516 end517 default :518 $display( " RTL-ERROR : %m : Illegal MFC0 cop0src register!" );519 endcase520 end522 // -- Illegal ---------------------------------------------------524 default :525 $display( " RTL-ERROR : %m : Illegal instruction !" );527 endcase529 //evaluate branch prediction530 Addr ppc = pcQ.first().qnxtpc; //predicted branch531 if (ppc != newPC) //prediction wrong532 begin533 epoch <= pcQ.first().qepoch + 1;534 bp.upd(instrpc, newPC); //update branch predictor535 pcQ.clear();536 pc <= newPC;537 end538 else539 pcQ.deq();540 //rlm: removing541 // if ( cp0_statsEn )542 // num_inst.incr();544 endrule546 rule writeback; // ( stage == Writeback );547 traceTiny("mkProc", "writeback","W");550 // get what to do off the writeback queue551 wbQ.deq();552 case (wbQ.first()) matches553 tagged WB_ALU {data:.res, dest:.rdst} : rf.wr(rdst, res);554 tagged WB_Load .regWr :555 begin556 dataRespQ.deq();557 if (dataRespQ.first() matches tagged LoadResp .ld)558 rf.wr(truncate(ld.tag), ld.data); // no need to use Rindx from queue? Duplicate?559 end560 tagged WB_Store : dataRespQ.deq();561 tagged WB_Host .dat : noAction;562 endcase564 endrule566 //rlm remove567 // rule inc_num_cycles;568 // if ( cp0_statsEn )569 // num_cycles.incr();570 // endrule572 /*573 // for now, we don't do anything.574 rule connectAudioReqResp;575 $display("rlm: PROCESSOR copies a datum\n");576 outAudioFifo.enq(inAudioFifo.first());577 inAudioFifo.deq;578 endrule579 */581 rule flagAudioEnd (inAudioFifo.first() matches tagged EndOfFile);582 $display (" PROCESSOR End Audio Flag Set ");583 cp0_audioEOF <= True;584 inAudioFifo.deq;585 endrule587 rule sendAudioEnd (cp0_progComp);588 $display (" PROCESSOR Says Program Complete ");589 outAudioFifo.enq(tagged EndOfFile);590 cp0_progComp <= False; //only send one. And functions to reset591 endrule594 //-----------------------------------------------------------595 // Methods597 interface Client imem_client;598 interface Get request = fifoToGet(instReqQ);599 interface Put response = fifoToPut(instRespQ);600 endinterface602 interface Client dmem_client;603 interface Get request = fifoToGet(dataReqQ);604 interface Put response = fifoToPut(dataRespQ);605 endinterface607 interface Get statsEn_get = toGet(asReg(cp0_statsEn));609 /*610 interface CPUToHost tohost;611 method Bit#(32) cpuToHost(int req);612 return (case (req)613 0: cp0_tohost;614 1: pc;615 2: zeroExtend(pack(stage));616 endcase);617 endmethod618 endinterface619 */621 interface Get sampleOutput = fifoToGet(outAudioFifo);622 interface Put sampleInput = fifoToPut(inAudioFifo);624 endmodule