<commit_before>
#include "TTASimControlRegion.h"
#include "TTASimDevice.h"

#include "accel-shared.h"

#include <SimpleSimulatorFrontend.hh>
//#include <SimulatorFrontend.hh>
#include <AddressSpace.hh>
#include <Machine.hh>

#include <math.h>

//#define ACCEL_TTASIM_DEBUG

TTASimControlRegion::TTASimControlRegion(const TTAMachine::Machine& mach, TTASimDevice* parent) {

#ifdef ACCEL_TTASIM_DEBUG
  POCL_MSG_PRINT_INFO("TTASim: Initializing TTASimControlRegion\n");
#endif
  PhysAddress = 0;
  Size = ACCEL_DEFAULT_CTRL_SIZE;
  parent_ = parent;
  assert(parent_ != nullptr &&
         "simulator parent handle NULL, is the sim opened properly?");

  setupControlRegisters(mach);
}

uint32_t TTASimControlRegion::Read32(size_t offset) {

#ifdef ACCEL_TTASIM_DEBUG
  POCL_MSG_PRINT_INFO("MMAP: Reading from physical address 0x%zx with "
                      "offset 0x%zx\n",
                      PhysAddress, offset);
#endif
  assert(offset < Size && "Attempt to access data outside MMAP'd buffer");
  auto value = ControlRegisters_[offset / sizeof(uint32_t)];
  return value;
}

void TTASimControlRegion::Write32(size_t offset, uint32_t value) {
#ifdef ACCEL_TTASIM_DEBUG
  POCL_MSG_PRINT_INFO("MMAP: Writing to physical address 0x%zx with "
                      "offset 0x%zx\n",
                      PhysAddress, offset);
#endif

  if (offset == ACCEL_CONTROL_REG_COMMAND) {
    switch(value) {
    case ACCEL_RESET_CMD:
      POCL_MSG_PRINT_INFO("parent %p",parent_);
      parent_->stopProgram();
      break;
    case ACCEL_CONTINUE_CMD:
      parent_->restartProgram();
      break;
    }
  }
}

void TTASimControlRegion::Write16(size_t offset, uint16_t value) {
  POCL_ABORT("Unimplemented 16bit writes to ttasimcontrolregion\n");
}

uint64_t TTASimControlRegion::Read64(size_t offset) {

#ifdef ACCEL_TTASIM_DEBUG
  POCL_MSG_PRINT_INFO("MMAP: Reading from physical address 0x%zx with "
                      "offset 0x%zx\n",
                      PhysAddress, offset);
#endif
  assert(offset < Size && "Attempt to access data outside MMAP'd buffer");
  auto value =
      reinterpret_cast<uint64_t *>(ControlRegisters_)[offset / sizeof(uint64_t)];
  return value;
}


void TTASimControlRegion::CopyToMMAP(size_t destination, const void *source,
                            size_t bytes) {
  POCL_ABORT("Unimplemented copytommap for ttasimcontrolregion\n");
}

void TTASimControlRegion::CopyFromMMAP(void *destination, size_t source, size_t bytes) {


  POCL_ABORT("Unimplemented copyfrommmap for ttasimcontrolregion\n");
}

void TTASimControlRegion::CopyInMem (size_t source, size_t destination, size_t bytes) {

  POCL_ABORT("Unimplemented copyinmem for ttasimcontrolregion\n");
}

void TTASimControlRegion::setupControlRegisters(const TTAMachine::Machine& mach)
{
  bool hasPrivateMem = false;
  bool sharedDataAndCq = false;
  bool relativeAddressing = true;
  int dmem_size = 0;
  int cq_size = 0;
  int imem_size = 0;
  const TTAMachine::Machine::AddressSpaceNavigator& nav =
    mach.addressSpaceNavigator();
  for (int i = 0; i < nav.count(); i++){
    TTAMachine::AddressSpace *as = nav.item(i);
    if (as->hasNumericalId(TTA_ASID_GLOBAL)) {
      if (as->end() == UINT32_MAX) {
        dmem_size = pow(2, 12); // TODO magic number from almaifintegrator.cc
        relativeAddressing = false;
      } else {
        dmem_size = as->end() + 1;
      }
      if (as->hasNumericalId(TTA_ASID_LOCAL)) {
        sharedDataAndCq = true;<commit_msg>[Accel] Increase default assumed global mem size<commit_after>
#include "TTASimControlRegion.h"
#include "TTASimDevice.h"

#include "accel-shared.h"

#include <SimpleSimulatorFrontend.hh>
//#include <SimulatorFrontend.hh>
#include <AddressSpace.hh>
#include <Machine.hh>

#include <math.h>

//#define ACCEL_TTASIM_DEBUG

TTASimControlRegion::TTASimControlRegion(const TTAMachine::Machine& mach, TTASimDevice* parent) {

#ifdef ACCEL_TTASIM_DEBUG
  POCL_MSG_PRINT_INFO("TTASim: Initializing TTASimControlRegion\n");
#endif
  PhysAddress = 0;
  Size = ACCEL_DEFAULT_CTRL_SIZE;
  parent_ = parent;
  assert(parent_ != nullptr &&
         "simulator parent handle NULL, is the sim opened properly?");

  setupControlRegisters(mach);
}

uint32_t TTASimControlRegion::Read32(size_t offset) {

#ifdef ACCEL_TTASIM_DEBUG
  POCL_MSG_PRINT_INFO("MMAP: Reading from physical address 0x%zx with "
                      "offset 0x%zx\n",
                      PhysAddress, offset);
#endif
  assert(offset < Size && "Attempt to access data outside MMAP'd buffer");
  auto value = ControlRegisters_[offset / sizeof(uint32_t)];
  return value;
}

void TTASimControlRegion::Write32(size_t offset, uint32_t value) {
#ifdef ACCEL_TTASIM_DEBUG
  POCL_MSG_PRINT_INFO("MMAP: Writing to physical address 0x%zx with "
                      "offset 0x%zx\n",
                      PhysAddress, offset);
#endif

  if (offset == ACCEL_CONTROL_REG_COMMAND) {
    switch(value) {
    case ACCEL_RESET_CMD:
      POCL_MSG_PRINT_INFO("parent %p",parent_);
      parent_->stopProgram();
      break;
    case ACCEL_CONTINUE_CMD:
      parent_->restartProgram();
      break;
    }
  }
}

void TTASimControlRegion::Write16(size_t offset, uint16_t value) {
  POCL_ABORT("Unimplemented 16bit writes to ttasimcontrolregion\n");
}

uint64_t TTASimControlRegion::Read64(size_t offset) {

#ifdef ACCEL_TTASIM_DEBUG
  POCL_MSG_PRINT_INFO("MMAP: Reading from physical address 0x%zx with "
                      "offset 0x%zx\n",
                      PhysAddress, offset);
#endif
  assert(offset < Size && "Attempt to access data outside MMAP'd buffer");
  auto value =
      reinterpret_cast<uint64_t *>(ControlRegisters_)[offset / sizeof(uint64_t)];
  return value;
}


void TTASimControlRegion::CopyToMMAP(size_t destination, const void *source,
                            size_t bytes) {
  POCL_ABORT("Unimplemented copytommap for ttasimcontrolregion\n");
}

void TTASimControlRegion::CopyFromMMAP(void *destination, size_t source, size_t bytes) {


  POCL_ABORT("Unimplemented copyfrommmap for ttasimcontrolregion\n");
}

void TTASimControlRegion::CopyInMem (size_t source, size_t destination, size_t bytes) {

  POCL_ABORT("Unimplemented copyinmem for ttasimcontrolregion\n");
}

void TTASimControlRegion::setupControlRegisters(const TTAMachine::Machine& mach)
{
  bool hasPrivateMem = false;
  bool sharedDataAndCq = false;
  bool relativeAddressing = true;
  int dmem_size = 0;
  int cq_size = 0;
  int imem_size = 0;
  const TTAMachine::Machine::AddressSpaceNavigator& nav =
    mach.addressSpaceNavigator();
  for (int i = 0; i < nav.count(); i++){
    TTAMachine::AddressSpace *as = nav.item(i);
    if (as->hasNumericalId(TTA_ASID_GLOBAL)) {
      if (as->end() == UINT32_MAX) {
        dmem_size = pow(2, 15); // TODO magic number from almaifintegrator.cc
        relativeAddressing = false;
      } else {
        dmem_size = as->end() + 1;
      }
      if (as->hasNumericalId(TTA_ASID_LOCAL)) {
        sharedDataAndCq = true;
{'fraction_non_alphanumeric': 0.10561056105610561, 'fraction_numerical': 0.011766394030707419, 'mean_word_length': 3.857142857142857, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4142160', 'n_tokens_mistral': 2469, 'n_tokens_neox': 2331, 'n_words': 505}