#ifndef _TEDMEM_H
#define _TEDMEM_H
#define RAMSIZE 65536
#define ROMSIZE 16384
#define SCR_HSIZE 456
#define SCR_VSIZE 312
#define TED_SOUND_CLOCK (221680)
#include "types.h"
#include "mem.h"
#include <stdio.h>
#include <string.h>
class CPU;
class KEYS;
class TAP;
class Filter;
class SIDsound;
class TED : public MemoryHandler {
public:
TED();
~TED();
KEYS *keys;
TAP *tap;
virtual void UpdateSerialState(unsigned char portval);
virtual void Reset();
virtual unsigned char Read(unsigned int addr);
virtual void Write(unsigned int addr, unsigned char value);
unsigned char readDMA(unsigned int addr) { return Ram[addr]; }
void wrtDMA(unsigned int addr, unsigned char value) { Ram[addr]=value; }
void setRamMask(unsigned int value) { RAMMask=value;}
unsigned int getRamMask(void) { return RAMMask;}
bool RAMenable;
bool bigram, bramsm;
void loadroms(void);
void loadloromfromfile(int nr, char fname[256]);
void loadhiromfromfile(int nr, char fname[256]);
char romlopath[4][256];
char romhighpath[4][256];
void dump(void *img);
void memin(void *img);
unsigned int beamx, beamy;
unsigned char screen[SCR_HSIZE*(SCR_VSIZE*2)];
bool render_ok;
void texttoscreen(int x,int y, const char *scrtxt);
void chrtoscreen(int x,int y, char scrchr);
unsigned int crsrpos;
int crsrphase;
bool crsrblinkon;
bool joyemu;
CPU *cpuptr;
void ted_process(short *buffer, unsigned int count);
void setDS( void *ds);
unsigned char Ram[RAMSIZE];
unsigned char RomHi[4][ROMSIZE];
void ChangeMemBankSetup(bool romoff);
bool t1on, t2on, t3on;
unsigned int timer1, timer2, timer3, t1start;
ClockCycle GetClockCount();
static TED *instance() { return instance_; };
void forcedReset();
void setMasterVolume(unsigned int shift);
void setSampleRate(unsigned int value);
void setFilterOrder(unsigned int value);
void initFilter(unsigned int sampleRate_, unsigned int filterOrder_);
unsigned int getSampleRate() { return sampleRate; };
void injectCodeToRAM(unsigned int address, unsigned char *from, size_t len);
void oscillatorInit();
void oscillatorReset();
void setPsidPlayAddress(unsigned int addr) { psidReplayAddr = addr; };
unsigned int getPsidPlayAddress() { return psidReplayAddr; };
void setPsid(bool isPsidFormat) { psidFormat = isPsidFormat; };
unsigned int isPsid() { return psidFormat; };
void selectWaveForm(unsigned int channel, unsigned int wave);
unsigned int getWaveForm(unsigned int channel) { return waveForm[channel]; }
void writeProtectedPlayerMemory(unsigned int addr, unsigned char *byte, unsigned int len) {
for(unsigned int i = 0; i < len; i++)
protectedPlayerMemory[(addr + i) & 0xfff] = *byte++;
}
void setplaybackSpeed(unsigned int speed);
void enableChannel(unsigned int channel, bool enable);
bool isChannelEnabled(unsigned int channel) {
return channelMask[channel % 3] != 0;
}
void storeToBuffer(short *buffer,unsigned int count = 1);
void copyToKbBuffer(char *bufferString, unsigned int bufferLength = -1);
unsigned int getTimeSinceLastReset();
void resetCycleCounter() { lastResetCycle = CycleCounter; }
SIDsound *getSidCard() { return sidCard; };
void enableSidCard(bool enable, unsigned int disableMask);
static unsigned int masterVolume;
int matrix[8*8] = {0};
int getKey(int x) {
if(currentKey >= 0) {
unsigned char res = 0;
for(int i=0; i<8; i++) {
if((x & (1<<i)) == 0) {
for(int j=0; j<8; j++) {
if(matrix[j+8*i])
res |= (1<<j);
}
}
}
return ~res;
}
return 0xff;
}
void putKey(int x) {
if(keyPos < 16)
keyBuffer[keyPos++] = x;
}
void miscUpdate() {
memset(matrix, 0, 8*8*4);
currentKey = -1;
if(keyPos > 0) {
currentKey = keyBuffer[0];
switch(currentKey) {
case '1':
matrix[0+8*7] = 1; break;
case '2':
matrix[3+8*7] = 1; break;
case '3':
matrix[0+8*1] = 1;
break;
case '4':
matrix[3+8*2] = 1;
break;
case '5':
matrix[0+8*2] = 1;
break;
case '6':
matrix[3+8*3] = 1;
break;
case '7':
matrix[0+8*3] = 1;
break;
case 0:
matrix[4+8*7] = 1; break;
case 1:
matrix[0+8*7] = 1; matrix[4+8*0] = 1; break;
case 2:
matrix[3+8*7] = 1;
matrix[5+8*0] = 1;
break;
case 3:
matrix[0+8*1] = 1;
matrix[6+8*0] = 1;
break;
case 4:
matrix[3+8*2] = 1;
break;
case 5:
matrix[0+8*2] = 1;
break;
case 6:
matrix[3+8*3] = 1;
break;
case 7:
matrix[0+8*3] = 1;
break;
}
memcpy(keyBuffer, keyBuffer + sizeof(int), keyPos * sizeof(int));
keyPos--;
}
}
private:
int keyBuffer[16];
int keyPos = 0;
int currentKey = 0;
static TED *instance_;
Filter *filter;
unsigned char RomLo[4][ROMSIZE];
unsigned char *actromlo, *actromhi;
unsigned char *mem_8000_bfff, *mem_c000_ffff, *mem_fc00_fcff;
unsigned int RAMMask;
unsigned char RamExt[4][RAMSIZE]; unsigned char *actram;
unsigned char prp, prddr;
unsigned char pio1;
bool scrblank;
unsigned int hshift, vshift;
unsigned int nrwscr, fltscr;
unsigned char DMAbuf[64*3];
unsigned char *chrbuf, *clrbuf, *tmpClrbuf;
int cposy;
void (TED::*scrmode)();
inline void hi_text();
void mc_text();
void mc_text_rvs();
void ec_text();
void mcec();
void rv_text();
void hi_bitmap();
void mc_bitmap();
void illegalbank();
bool charrom;
int rvsmode, grmode, ecmode;
int scrattr, charbank;
unsigned int framecol;
unsigned char *colorbank, *charrombank, *charrambank;
unsigned char *grbank;
unsigned char *scrptr, *endptr, *ramptr;
const char *DMAptr;
unsigned int fastmode, irqline;
unsigned char hcol[2], mcol[4], ecol[4], bmmcol[4], *cset;
static ClockCycle CycleCounter;
ClockCycle lastResetCycle;
void DoDMA( unsigned char *Buf, unsigned int Offset );
void setFreq(unsigned int channel, int freq);
void writeSoundReg(unsigned int reg, unsigned char value);
void renderSound(unsigned int nrsamples, short *buffer);
unsigned int psidReplayAddr;
bool psidFormat;
unsigned int waveForm[2];
unsigned int getWaveSample(unsigned int channel, unsigned int wave);
unsigned int waveSquare(unsigned int channel);
unsigned int waveSawTooth(unsigned int channel);
unsigned int waveTriangle(unsigned int channel);
unsigned char protectedPlayerMemory[0x1000];
unsigned int playbackSpeed;
unsigned int channelMask[3];
unsigned int sampleRate;
unsigned int filterOrder;
SIDsound *sidCard;
};
const short HUE[16] = { 0, 0,
103, 283,
53, 241, 347,
167,123, 148,
195, 83, 265,
323, 355, 213
};
const double luma[9] = {
2.00, 2.4, 2.55, 2.7, 2.9, 3.3, 3.6, 4.1, 4.8 };
#endif