|
| 1 | +// Copyright lowRISC contributors. |
| 2 | +// Licensed under the Apache License, Version 2.0, see LICENSE for details. |
| 3 | +// SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +#include <string> |
| 6 | +#include <vector> |
| 7 | + |
| 8 | +#ifndef COSIM_H_ |
| 9 | +#define COSIM_H_ |
| 10 | + |
| 11 | +// Information about a dside transaction observed on the DUT memory interface |
| 12 | +struct DSideAccessInfo { |
| 13 | + // set when the access is a store, in which case `data` is the store data from |
| 14 | + // the DUT. Otherwise `data` is the load data provided to the DUT. |
| 15 | + bool store; |
| 16 | + // `addr` is the address and must be 32-bit aligned. `data` and `be` are |
| 17 | + // aligned to the address. For example an 8-bit store of 0xff to 0x12 has |
| 18 | + // `data` as 0x00ff0000, `addr` as 0x10 and `be` as 0b0100. |
| 19 | + uint32_t data; |
| 20 | + uint32_t addr; |
| 21 | + uint32_t be; |
| 22 | + |
| 23 | + // set if an error response to the transaction is seen. |
| 24 | + bool error; |
| 25 | + |
| 26 | + // `misaligned_first` and `misaligned_second` are set when the transaction is |
| 27 | + // generated for a misaligned store or load instruction. `misaligned_first` |
| 28 | + // is true when the transaction is for the lower half and `misaligned_second` |
| 29 | + // is true when the transaction is for the upper half, if it exists. |
| 30 | + // |
| 31 | + // For example an unaligned 32-bit load to 0x3 produces a transaction with |
| 32 | + // `addr` as 0x0 and `misaligned_first` set to true, then a transaction with |
| 33 | + // `addr` as 0x4 and `misaligned_second` set to true. An unaligned 16-bit load |
| 34 | + // to 0x01 only produces a transaction with `addr` as 0x0 and |
| 35 | + // `misaligned_first` set to true, there is no second half. |
| 36 | + bool misaligned_first; |
| 37 | + bool misaligned_second; |
| 38 | +}; |
| 39 | + |
| 40 | +class Cosim { |
| 41 | + public: |
| 42 | + virtual ~Cosim() {} |
| 43 | + |
| 44 | + // Add a memory to the co-simulator environment. |
| 45 | + // |
| 46 | + // Use `backdoor_write_mem`/`backdoor_read_mem` to access it from the |
| 47 | + // simulation environment. |
| 48 | + virtual void add_memory(uint32_t base_addr, size_t size) = 0; |
| 49 | + |
| 50 | + // Write bytes to co-simulator memory. |
| 51 | + // |
| 52 | + // returns false if write fails (e.g. because no memory exists at the bytes |
| 53 | + // being written). |
| 54 | + virtual bool backdoor_write_mem(uint32_t addr, size_t len, |
| 55 | + const uint8_t *data_in) = 0; |
| 56 | + |
| 57 | + // Read bytes from co-simulator memory. |
| 58 | + // |
| 59 | + // returns false if read fails (e.g. because no memory exists at the bytes |
| 60 | + // being read). |
| 61 | + virtual bool backdoor_read_mem(uint32_t addr, size_t len, |
| 62 | + uint8_t *data_out) = 0; |
| 63 | + |
| 64 | + // Step the co-simulator, checking register write and PC of executed |
| 65 | + // instruction match the supplied values. `write_reg` gives the index of the |
| 66 | + // written register along with `write_reg_data` which provides the data. A |
| 67 | + // `write_reg` of 0 indicates no register write occurred. |
| 68 | + // |
| 69 | + // `sync_trap` is set to true when the instruction caused a synchronous trap. |
| 70 | + // In this case the instruction doesn't retire so no register write occurs (so |
| 71 | + // `write_reg` must be 0). |
| 72 | + // |
| 73 | + // Returns false if there are any errors; use `get_errors` to obtain details |
| 74 | + virtual bool step(uint32_t write_reg, uint32_t write_reg_data, uint32_t pc, |
| 75 | + bool sync_trap) = 0; |
| 76 | + |
| 77 | + // When more than one of `set_mip`, `set_nmi` or `set_debug_req` is called |
| 78 | + // before `step` which one takes effect is chosen by the co-simulator. Which |
| 79 | + // should take priority is architecturally defined by the RISC-V |
| 80 | + // specification. |
| 81 | + |
| 82 | + // Set the value of MIP. |
| 83 | + // |
| 84 | + // At the next call of `step`, the MIP value will take effect (i.e. if it's a |
| 85 | + // new interrupt that is enabled it will step straight to that handler). |
| 86 | + virtual void set_mip(uint32_t mip) = 0; |
| 87 | + |
| 88 | + // Set the state of the NMI (non-maskable interrupt) line. |
| 89 | + // |
| 90 | + // The NMI signal is a level triggered interrupt. When the NMI is taken the |
| 91 | + // CPU ignores the NMI line until an `mret` instruction is executed. If the |
| 92 | + // NMI is high following the `mret` (regardless of whether it has been low or |
| 93 | + // not whilst the first NMI is being handled) a new NMI is taken. |
| 94 | + // |
| 95 | + // When an NMI is due to be taken that will occur at the next call of `step`. |
| 96 | + virtual void set_nmi(bool nmi) = 0; |
| 97 | + |
| 98 | + // Set the debug request. |
| 99 | + // |
| 100 | + // When set to true the core will enter debug mode at the next step |
| 101 | + virtual void set_debug_req(bool debug_req) = 0; |
| 102 | + |
| 103 | + // Set the value of mcycle. |
| 104 | + // |
| 105 | + // The co-simulation model doesn't alter the value of mcycle itself (other |
| 106 | + // than instructions that do a direct CSR write). mcycle should be set to the |
| 107 | + // correct value before any `step` call that may execute an instruction that |
| 108 | + // observes the value of mcycle. |
| 109 | + // |
| 110 | + // A full 64-bit value is provided setting both the mcycle and mcycleh CSRs. |
| 111 | + virtual void set_mcycle(uint64_t mcycle) = 0; |
| 112 | + |
| 113 | + // Tell the co-simulation model about observed transactions on the dside |
| 114 | + // memory interface of the DUT. Accesses are notified once the response to a |
| 115 | + // transaction is seen. |
| 116 | + // |
| 117 | + // Observed transactions for the DUT are checked against accesses from the |
| 118 | + // co-simulation model when a memory access occurs during a `step`. If there |
| 119 | + // is a mismatch an error is reported which can be obtained via `get_errors`. |
| 120 | + virtual void notify_dside_access(const DSideAccessInfo &access_info) = 0; |
| 121 | + |
| 122 | + // Tell the co-simulation model about an error response to an iside fetch. |
| 123 | + // |
| 124 | + // `addr` must be 32-bit aligned. |
| 125 | + // |
| 126 | + // The next step following a call to `set_iside_error` must produce an |
| 127 | + // instruction fault at the given address. |
| 128 | + virtual void set_iside_error(uint32_t addr) = 0; |
| 129 | + |
| 130 | + // Get a vector of strings describing errors that have occurred during `step` |
| 131 | + virtual const std::vector<std::string> &get_errors() = 0; |
| 132 | + |
| 133 | + // Clear internal vector of error descriptions |
| 134 | + virtual void clear_errors() = 0; |
| 135 | +}; |
| 136 | + |
| 137 | +#endif // COSIM_H_ |
0 commit comments