CompiledVirtualMachine.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. Copyright (c) 2018 tevador
  3. This file is part of RandomX.
  4. RandomX is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. RandomX is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with RandomX. If not, see<http://www.gnu.org/licenses/>.
  14. */
  15. #include "CompiledVirtualMachine.hpp"
  16. #include "Pcg32.hpp"
  17. #include "common.hpp"
  18. #include "instructions.hpp"
  19. #include <stdexcept>
  20. namespace RandomX {
  21. CompiledVirtualMachine::CompiledVirtualMachine() {
  22. totalSize = 0;
  23. }
  24. void CompiledVirtualMachine::setDataset(dataset_t ds) {
  25. mem.ds = ds;
  26. }
  27. void CompiledVirtualMachine::initializeScratchpad(uint8_t* scratchpad, int32_t index) {
  28. memcpy(scratchpad, mem.ds.dataset + ScratchpadSize * index, ScratchpadSize);
  29. }
  30. void CompiledVirtualMachine::initializeProgram(const void* seed) {
  31. Pcg32 gen(seed);
  32. for (unsigned i = 0; i < sizeof(reg) / sizeof(Pcg32::result_type); ++i) {
  33. *(((uint32_t*)&reg) + i) = gen();
  34. }
  35. FPINIT();
  36. for (int i = 0; i < RegistersCount; ++i) {
  37. reg.f[i].lo.f64 = (double)reg.f[i].lo.i64;
  38. reg.f[i].hi.f64 = (double)reg.f[i].hi.i64;
  39. }
  40. compiler.generateProgram(gen);
  41. mem.ma = (gen() ^ *(((uint32_t*)seed) + 4)) & ~7;
  42. mem.mx = *(((uint32_t*)seed) + 5);
  43. }
  44. void CompiledVirtualMachine::execute() {
  45. //executeProgram(reg, mem, scratchpad, readDataset);
  46. totalSize += compiler.getCodeSize();
  47. compiler.getProgramFunc()(reg, mem, scratchpad);
  48. #ifdef TRACEVM
  49. for (int32_t i = InstructionCount - 1; i >= 0; --i) {
  50. std::cout << std::hex << tracepad[i].u64 << std::endl;
  51. }
  52. #endif
  53. }
  54. }