dsp1emu.hpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // DSP-1's emulation code
  2. //
  3. // Based on research by Overload, The Dumper, Neviksti and Andreas Naive
  4. // Date: June 2006
  5. #ifndef __DSP1EMUL_H
  6. #define __DSP1EMUL_H
  7. #define DSP1_VERSION 0x0102
  8. class Dsp1
  9. {
  10. public:
  11. // The DSP-1 status register has 16 bits, but only
  12. // the upper 8 bits can be accessed from an external device, so all these
  13. // positions are referred to the upper byte (bits D8 to D15)
  14. enum SrFlags {DRC=0x04, DRS=0x10, RQM=0x80};
  15. // According to Overload's docs, these are the meanings of the flags:
  16. // DRC: The Data Register Control (DRC) bit specifies the data transfer length to and from the host CPU.
  17. // 0: Data transfer to and from the DSP-1 is 16 bits.
  18. // 1: Data transfer to and from the DSP-1 is 8 bits.
  19. // DRS: The Data Register Status (DRS) bit indicates the data transfer status in the case of transfering 16-bit data.
  20. // 0: Data transfer has terminated.
  21. // 1: Data transfer in progress.
  22. // RQM: The Request for Master (RQM) indicates that the DSP1 is requesting host CPU for data read/write.
  23. // 0: Internal Data Register Transfer.
  24. // 1: External Data Register Transfer.
  25. Dsp1();
  26. uint8 getSr(); // return the status register's high byte
  27. uint8 getDr();
  28. void setDr(uint8 iDr);
  29. void reset();
  30. private:
  31. enum FsmMajorState {WAIT_COMMAND, READ_DATA, WRITE_DATA};
  32. enum MaxDataAccesses {MAX_READS=7, MAX_WRITES=1024};
  33. struct Command {
  34. void (Dsp1::*callback)(int16 *, int16 *);
  35. unsigned int reads;
  36. unsigned int writes;
  37. };
  38. static const Command mCommandTable[];
  39. static const int16 MaxAZS_Exp[16];
  40. static const int16 SinTable[];
  41. static const int16 MulTable[];
  42. static const uint16 DataRom[];
  43. struct SharedData { // some RAM variables shared between commands
  44. int16 MatrixA[3][3]; // attitude matrix A
  45. int16 MatrixB[3][3];
  46. int16 MatrixC[3][3];
  47. int16 CentreX, CentreY, CentreZ; // center of projection
  48. int16 CentreZ_C, CentreZ_E;
  49. int16 VOffset; // vertical offset of the screen with regard to the centre of projection
  50. int16 Les, C_Les, E_Les;
  51. int16 SinAas, CosAas;
  52. int16 SinAzs, CosAzs;
  53. int16 SinAZS, CosAZS;
  54. int16 SecAZS_C1, SecAZS_E1;
  55. int16 SecAZS_C2, SecAZS_E2;
  56. int16 Nx, Ny, Nz; // normal vector to the screen (norm 1, points toward the center of projection)
  57. int16 Gx, Gy, Gz; // center of the screen (global coordinates)
  58. int16 Hx, Hy; // horizontal vector of the screen (Hz=0, norm 1, points toward the right of the screen)
  59. int16 Vx, Vy, Vz; // vertical vector of the screen (norm 1, points toward the top of the screen)
  60. } shared;
  61. uint8 mSr; // status register
  62. int mSrLowByteAccess;
  63. uint16 mDr; // "internal" representation of the data register
  64. FsmMajorState mFsmMajorState; // current major state of the FSM
  65. uint8 mCommand; // current command processed by the FSM
  66. uint8 mDataCounter; // #uint16 read/writes counter used by the FSM
  67. int16 mReadBuffer[MAX_READS];
  68. int16 mWriteBuffer[MAX_WRITES];
  69. bool mFreeze; // need explanation? ;)
  70. void fsmStep(bool read, uint8 &data); // FSM logic
  71. // commands
  72. void memoryTest(int16 *input, int16 *output);
  73. void memoryDump(int16 *input, int16 *output);
  74. void memorySize(int16 *input, int16 *output);
  75. void multiply(int16* input, int16* output);
  76. void multiply2(int16* input, int16* output);
  77. void inverse(int16 *input, int16 *output);
  78. void triangle(int16 *input, int16 *output);
  79. void radius(int16 *input, int16 *output);
  80. void range(int16 *input, int16 *output);
  81. void range2(int16 *input, int16 *output);
  82. void distance(int16 *input, int16 *output);
  83. void rotate(int16 *input, int16 *output);
  84. void polar(int16 *input, int16 *output);
  85. void attitudeA(int16 *input, int16 *output);
  86. void attitudeB(int16 *input, int16 *output);
  87. void attitudeC(int16 *input, int16 *output);
  88. void objectiveA(int16 *input, int16 *output);
  89. void objectiveB(int16 *input, int16 *output);
  90. void objectiveC(int16 *input, int16 *output);
  91. void subjectiveA(int16 *input, int16 *output);
  92. void subjectiveB(int16 *input, int16 *output);
  93. void subjectiveC(int16 *input, int16 *output);
  94. void scalarA(int16 *input, int16 *output);
  95. void scalarB(int16 *input, int16 *output);
  96. void scalarC(int16 *input, int16 *output);
  97. void gyrate(int16 *input, int16 *output);
  98. void parameter(int16 *input, int16 *output);
  99. void raster(int16 *input, int16 *output);
  100. void target(int16 *input, int16 *output);
  101. void project(int16 *input, int16 *output);
  102. // auxiliar functions
  103. int16 sin(int16 Angle);
  104. int16 cos(int16 Angle);
  105. void inverse(int16 Coefficient, int16 Exponent, int16 &iCoefficient, int16 &iExponent);
  106. int16 denormalizeAndClip(int16 C, int16 E);
  107. void normalize(int16 m, int16 &Coefficient, int16 &Exponent);
  108. void normalizeDouble(int32 Product, int16 &Coefficient, int16 &Exponent);
  109. int16 shiftR(int16 C, int16 E);
  110. };
  111. #endif