Minres / SystemC-Components

A SystemC productivity library: https://minres.github.io/SystemC-Components/
https://www.minres.com/#opensource
Apache License 2.0
81 stars 21 forks source link

Add support for word-addressed registers #24

Closed tk-ka closed 2 years ago

tk-ka commented 2 years ago

Currently, only byte-addressed resources can be used with scc::tlm_target. The underlying type util::range_lut throws a std::runtime_error("range already mapped") if word-addressed entries are added.

Use case: Integrated circuits (discrete components) typically communicate over serial interfaces (I2C, SPI, ...) and use word addressing for the registers. To model this properly with TLM, SCC should support this.

The issue can be reproduced with the following register container class.

class test_regs : public sc_core::sc_module, public scc::resetable
{
public:
  uint32_t reg_one;
  uint32_t reg_two;
  uint32_t reg_three;
  uint32_t reg_four;

  scc::sc_register<uint32_t> one;
  scc::sc_register<uint32_t> two;
  scc::sc_register<uint32_t> three;
  scc::sc_register<uint32_t> four;

  test_regs(sc_core::sc_module_name name) :
      sc_core::sc_module(name),
      NAMED(one, reg_one, 1111, *this),
      NAMED(two, reg_two, 2222, *this),
      NAMED(three, reg_three, 3333, *this)
      NAMED(four, reg_four, 4444, *this)
  { }

    inline void registerResources(scc::tlm_target<32> &target)
    {
      target.addResource(one, 0x01);
      target.addResource(two, 0x02);
      target.addResource(three, 0x03);
      target.addResource(four, 0x04);
    }
};

Note: Multiplying the real addresses by the byte-size of the registers is a work-around but not a decent solution.

eyck commented 2 years ago

Actually addresses are always treated as byte addresses by convention since this is the smallest common denominator amongst the variety of systems. This also applies to the registration of resources at the sockets. So your workaround is not a workaround it is the correct implementation. The regstration and hence the resulting address map is independent of the way it is being accessed via the socket. I do not see why this is not a decent solution. Pls. explain.

tk-ka commented 2 years ago

I don't see byte-addressing as the one-and-only convention because both ways are used in applications and so both are equally valid. Feel free to pick a datasheet of an arbitrary IC with a digital interface and take a look at the address map. The workaround is no decent implementation because you are forced to build model in which the registers will have a different address than it is in reality (IP documentation, user-perspective). This deviation is an obvious source of error when developing, maintaining and debugging the model. And in general: It is the cleaner approach to adapt the tool (SCC) to fit to the modelling task instead of making the model fit to the tool.

tk-ka commented 2 years ago

Thank you @staskau for that commit! The new template arguments works for me with GCC under Linux but I experienced a problem with MSVC. See my comment

eyck commented 2 years ago

fixed the std::max compile isse with 9b587f3