Skip to content

Conversation

@challenger1024
Copy link

@challenger1024 challenger1024 commented Nov 23, 2025

Add a configuration option to control the behaviour when executing AMOCAS instructions that are reserved due to the use of odd register indices.

This is the first setting we've added to control reserved behaviour. It allows two options: a fatal error that stops the simulation, or to treat it as an illegal instruction (the default choice).

See #775

@Timmmm
Copy link
Collaborator

Timmmm commented Nov 23, 2025

Nice! I think maybe it would make more sense to have a fine grained config where the behaviour for each kind of reserved behaviour can be controlled. The spec isn't very clear about the semantics of reserved behaviour in general (sometimes it is UNSPECIFIED but not always), so I think it's easiest if we're explicit about it.

Also we probably want an enum rather than a bool to allow more possible behaviour options in future. Something like:

"reserved_behavior": {
  "nonexistent_csr": "illegal"/"read_only_zero"/"exception",
"amocas_odd_registers": "illegal"/"exception",
"reserved_fence": ...

@Timmmm
Copy link
Collaborator

Timmmm commented Nov 23, 2025

Also I reckon we should add another function to use for this instead of internal_error, and probably another exception type.

@jordancarlin
Copy link
Collaborator

Beyond @Timmmm's comments, I think we should also avoid using the term "strict" to describe the mode that terminates the simulation because of potential confusion with the Ssstrict extension (which requires most reserved behavior to throw an exception).

@challenger1024
Copy link
Author

Changes Introduced in the new Commit

  • Updated the configuration item in config.json.in to define reserved_behaviour as an enumeration type. At present, it includes only one entry — amocas_odd_registers , which can take the following values:
    • illegal : returns Illegal_Instruction
    • ignore : ignores amocas_odd_registers and continues executing the remaining part of the AMO function
  • Replaced the call to internal_error in model/extensions/A/zaamo_insts.sail with return Illegal_Instruction()

Questions for Clarification

  • Would it be appropriate to introduce a dedicated exception type for reserved_behaviour?For instance, when config base.reserved_behaviour.amocas_odd_registers = "exception", we could return a specific exception type, such as:
    union Reserved_Behaviour {
      Amocas_odd_registers : unit,
      ...
    }
  • Should we implement a unified handler function to manage exceptions raised by reserved behaviours,or would it be preferable to define a dedicated handler for each reserved behaviour individually, for example:
    function handle_reserved_behaviour(reserved_behaviour : Reserved_Behaviour) {
    
    }

@Timmmm

@challenger1024 challenger1024 force-pushed the amocas branch 2 times, most recently from 7e84535 to 058e0dd Compare November 28, 2025 04:33
@challenger1024 challenger1024 changed the title Add reserved_behaviour_strict config and strict mode check for AMOCAS… Add reserved_behaviour.AMOCAS_odd_registers config Nov 28, 2025
Copy link
Collaborator

@Timmmm Timmmm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be appropriate to introduce a dedicated exception type for reserved_behaviour?For instance, when config base.reserved_behaviour.amocas_odd_registers = "exception", we could return a specific exception type, such as:

Sounds like a good idea to me!

Should we implement a unified handler function to manage exceptions raised by reserved behaviours,or would it be preferable to define a dedicated handler for each reserved behaviour individually, for example:

Yeah I think so, like the existing internal_error function.

@challenger1024
Copy link
Author

challenger1024 commented Dec 5, 2025

After reviewing the sail-riscv source code, I found that when the registers
are odd-numbered, amo_encoding_valid() already returns false for AMOCAS.D/Q.
As a result, the model treats these encodings as illegal instructions since
the AMO mapping becomes invalid. Therefore, I focused only on handling the
case when the configuration is set to "exit", defining how the model should
terminate in that situation.
latest commit ,Move odd-register check for AMOCAS.D/Q into amo_encoding_valid()
@Timmmm

Copy link
Collaborator

@Timmmm Timmmm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some code fixes needed but the idea looks good to me now.

I'm curious what the motivation for adding the flag for this particular reserved behaviour is, or was it just randomly picked from the list in #775 ?

))
width <= xlen_bytes | (
(op == AMOCAS & width <= xlen_bytes * 2 )
&
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Alasdair does Sail guarantee short-circuit boolean operator semantics? Presumably yes; just thought I'd double check.

@challenger1024
Copy link
Author

In the latest commit, I added the ReservedBehaviourPolicy enum and the reserved_behaviour function.
First, to answer your question above: selecting an AMOCAS odd-number register is currently random, but if possible, I would like to extend the configuration to include other reserved behaviours mentioned in #775 as well.
In addition, I have a few questions:

  1. I’m not entirely sure whether placing ReservedBehaviourPolicy in types.sail is the best choice — is there a more appropriate location?
  2. Is using the name ReservedBehaviourPolicy suitable, or would you suggest a better alternative?
  3. For the enum members of ReservedBehaviourPolicy, should I rename them to RB_Exit or RB_Illegal to avoid potential naming conflicts with other occurrences of Illegal?
    @Timmmm

Copy link
Collaborator

@Timmmm Timmmm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, just a few minor tweaks.

Also we might want to use the barbarian American spelling of behaviour without the u. They do have nukes...

@challenger1024
Copy link
Author

last commit,Change behaviour to behavior, change Exit to Fatal, and make other minor adjustments

@Timmmm

Copy link
Collaborator

@Timmmm Timmmm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

@github-actions
Copy link

github-actions bot commented Dec 10, 2025

Test Results

2 115 tests   2 115 ✅  17m 54s ⏱️
    1 suites      0 💤
    1 files        0 ❌

Results for commit 84b34f5.

♻️ This comment has been updated with latest results.

@challenger1024
Copy link
Author

last commit, Remove previously ignored redundant function declaration: reserved_behavior_exit
@Timmmm

@challenger1024
Copy link
Author

last commit, change enum name ReservedBehaviorPolicy to AmocasOddRegisterReservedBehavior.
@Timmmm

@challenger1024
Copy link
Author

Hello, I’ve tried adding the configuration option into platform_config.sail, but I ran into some issues.
My code looks like this:

let amocas_odd_register_reserved_behavior : AmocasOddRegisterReservedBehavior = config base.reserved_behavior.amocas_odd_register

However, since the AmocasOddRegisterReservedBehavior enum is defined in types.sail, and in model/riscv.sail_project the types.sail file is listed after platform_config.sail, platform_config.sail cannot find AmocasOddRegisterReservedBehavior.
Do you have any suggestions for how to resolve this issue?
Should I move AmocasOddRegisterReservedBehavior to another file, and if so, where would be the best place to put it?
@pmundkur

@Timmmm
Copy link
Collaborator

Timmmm commented Dec 19, 2025

Should I move AmocasOddRegisterReservedBehavior to another file, and if so, where would be the best place to put it?

I think it's probably fine to move it to platform_config.sail.

@pmundkur
Copy link
Collaborator

Yeah, moving to platform_config is probably okay for now. We should refactor types.sail at some point. It depends on extensions now due to Ext_H which is not good for a file called types.

@challenger1024
Copy link
Author

last commit,
Move AmocasOddRegisterReservedBehavior from types.sail to platform_config.sail, and add the variable amocas_odd_register_reserved_behavior in platform_config.sail. Removed extra blank lines in errors.sail.

Copy link
Collaborator

@Timmmm Timmmm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

challenger1024 and others added 19 commits December 23, 2025 12:15
Signed-off-by: Tim Hutt <[email protected]>
…nfig.sail, and add the variable amocas_odd_register_reserved_behavior in platform_config.sail.
Signed-off-by: Tim Hutt <[email protected]>
Copy link
Collaborator

@pmundkur pmundkur left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added some minor cleanup and rebased.

@Timmmm Timmmm added the will be merged Scheduled to be merged soon if nobody objects label Dec 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

will be merged Scheduled to be merged soon if nobody objects

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants