Skip to content

Commit

Permalink
Add cycle(h) and instret(h)
Browse files Browse the repository at this point in the history
Partial fix to #2
  • Loading branch information
TheThirdOne committed May 17, 2019
1 parent 3054cbb commit 522ebbd
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 6 deletions.
30 changes: 29 additions & 1 deletion rars/riscv/hardware/ControlAndStatusRegisterFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,11 @@ public class ControlAndStatusRegisterFile {
new Register("uepc", 0x041, 0),
new Register("ucause", 0x042, 0),
new Register("utval", 0x043, 0),
new Register("uip", 0x044, 0)
new Register("uip", 0x044, 0),
new ReadOnlyRegister("cycle", 0xC00, 0, -1),
new ReadOnlyRegister("instret",0xC02, 0, -1),
new ReadOnlyRegister("cycleh", 0xC80, 0, -1),
new ReadOnlyRegister("instreth",0xC81, 0, -1),
};
tmp[1] = new LinkedRegister("fflags", 0x001, tmp[3], 0x1F);
tmp[2] = new LinkedRegister("frm", 0x002, tmp[3], 0xE0);
Expand Down Expand Up @@ -92,6 +96,30 @@ public static int updateRegister(String name, int val) {
return updateRegister(instance.getRegister(name).getNumber(), val);
}

/**
* This method updates the register value silently and bypasses read only
*
* @param num Number of register to set the value of.
* @param val The desired value for the register.
* @return old value in register prior to update
**/
public static int updateRegisterBackdoor(int num, int val) {
return (Globals.getSettings().getBackSteppingEnabled())
? Globals.program.getBackStepper().addControlAndStatusBackdoor(num, instance.getRegister(num).setValueBackdoor(val))
: instance.getRegister(num).setValueBackdoor(val);
}

/**
* This method updates the register value silently and bypasses read only
*
* @param name Name of register to set the value of.
* @param val The desired value for the register.
* @return old value in register prior to update
**/
public static int updateRegisterBackdoor(String name, int val) {
return updateRegisterBackdoor(instance.getRegister(name).getNumber(), val);
}

/**
* ORs a register with a value
*
Expand Down
52 changes: 52 additions & 0 deletions rars/riscv/hardware/ReadOnlyRegister.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package rars.riscv.hardware;

/*
Copyright (c) 2019, Benjamin Landers
Developed by Benjamin Landers ([email protected])
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject
to the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
(MIT license, http://www.opensource.org/licenses/mit-license.html)
*/

/**
* A register which aliases a subset of another register
*/
public class ReadOnlyRegister extends Register {
private int mask;

/**
* @param name the name to assign
* @param num the number to assign
* @param val the reset value
* @param mask the bits to use
*/
public ReadOnlyRegister(String name, int num, int val, int mask) {
super(name, num, val); // reset value does not matter
this.mask = mask;
}

public synchronized int setValue(int val) {
int current = getValue();
super.setValue((current & mask) | (val & ~mask));
return current;
}
}
15 changes: 15 additions & 0 deletions rars/riscv/hardware/Register.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,21 @@ public synchronized int setValue(int val) {
return old;
}

/**
* Sets the value of the register to the val passed to it. This should only
* be used to update registers not related to the current instruction.
*
* @param val Value to set the Register to.
* @return previous value of register
*/

public synchronized int setValueBackdoor(int val) {
int old = value;
value = val;
return old;
}


/**
* Resets the value of the register to the value it was constructed with.
* Observers are not notified.
Expand Down
19 changes: 19 additions & 0 deletions rars/simulator/BackStepper.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ private enum Action {
REGISTER_RESTORE,
PC_RESTORE,
CONTROL_AND_STATUS_REGISTER_RESTORE,
CONTROL_AND_STATUS_REGISTER_BACKDOOR,
FLOATING_POINT_REGISTER_RESTORE,
DO_NOTHING
}
Expand Down Expand Up @@ -155,6 +156,9 @@ public void backStep() {
case CONTROL_AND_STATUS_REGISTER_RESTORE:
ControlAndStatusRegisterFile.updateRegister(step.param1,step.param2);
break;
case CONTROL_AND_STATUS_REGISTER_BACKDOOR:
ControlAndStatusRegisterFile.updateRegisterBackdoor(step.param1,step.param2);
break;
case PC_RESTORE:
RegisterFile.setProgramCounter(step.param1);
break;
Expand Down Expand Up @@ -275,6 +279,21 @@ public int addControlAndStatusRestore(int register, int value) {
return value;
}


/**
* Add a new "back step" (the undo action) to the stack. The action here
* is to restore a control and status register value. This does not obey
* read only restrictions and does not notify observers.
*
* @param register The affected register number.
* @param value The "restore" value to be stored there.
* @return the argument value
*/
public int addControlAndStatusBackdoor(int register, int value) {
backSteps.push(Action.CONTROL_AND_STATUS_REGISTER_BACKDOOR, pc(), register, value);
return value;
}

/**
* Add a new "back step" (the undo action) to the stack. The action here
* is to restore a floating point register value.
Expand Down
23 changes: 18 additions & 5 deletions rars/simulator/Simulator.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package rars.simulator;

import rars.*;
import rars.riscv.hardware.AddressErrorException;
import rars.riscv.hardware.ControlAndStatusRegisterFile;
import rars.riscv.hardware.InterruptController;
import rars.riscv.hardware.RegisterFile;
import rars.riscv.hardware.*;
import rars.riscv.BasicInstruction;
import rars.riscv.Instruction;
import rars.util.Binary;
Expand Down Expand Up @@ -519,7 +516,23 @@ public void run() {
}
}// end synchronized block

// Return if we've reached a breakpoint.
// Update cycle(h) and instret(h)
int cycle = ControlAndStatusRegisterFile.getValueNoNotify("cycle"),
instret = ControlAndStatusRegisterFile.getValueNoNotify("instret");
ControlAndStatusRegisterFile.updateRegisterBackdoor("cycle",cycle+1);
if(cycle == -1){
int cycleh = ControlAndStatusRegisterFile.getValueNoNotify("cycleh");
ControlAndStatusRegisterFile.updateRegisterBackdoor("cycleh",cycleh+1);
}

ControlAndStatusRegisterFile.updateRegisterBackdoor("instret",instret+1);
if(instret == -1){
int instreth = ControlAndStatusRegisterFile.getValueNoNotify("instreth");
ControlAndStatusRegisterFile.updateRegisterBackdoor("instreth",instreth+1);
}


// Return if we've reached a breakpoint.
if (ebreak || (breakPoints != null) &&
(Arrays.binarySearch(breakPoints, RegisterFile.getProgramCounter()) >= 0)) {
stopExecution(false, Reason.BREAKPOINT);
Expand Down

0 comments on commit 522ebbd

Please sign in to comment.