| 1 |
|
| 2 |
// ep128emu -- portable Enterprise 128 emulator |
| 3 |
// Copyright (C) 2003-2016 Istvan Varga <[email protected]> |
| 4 |
// https://github.com/istvan-v/ep128emu/ |
| 5 |
// |
| 6 |
// This program is free software; you can redistribute it and/or modify |
| 7 |
// it under the terms of the GNU General Public License as published by |
| 8 |
// the Free Software Foundation; either version 2 of the License, or |
| 9 |
// (at your option) any later version. |
| 10 |
// |
| 11 |
// This program is distributed in the hope that it will be useful, |
| 12 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
// GNU General Public License for more details. |
| 15 |
// |
| 16 |
// You should have received a copy of the GNU General Public License |
| 17 |
// along with this program; if not, write to the Free Software |
| 18 |
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 |
|
| 20 |
#ifndef EP128EMU_DAVE_HPP |
| 21 |
#define EP128EMU_DAVE_HPP |
| 22 |
|
| 23 |
#include <stdint.h> |
| 24 |
|
| 25 |
namespace Ep128 { |
| 26 |
|
| 27 |
class DaveTables { |
| 28 |
public: |
| 29 |
// tables for polynomial counters |
| 30 |
// (note: the table data is stored in reverse order) |
| 31 |
uint8_t *polycnt4_table; // length = 15, poly = 0x0000000C |
| 32 |
uint8_t *polycnt5_table; // length = 31, poly = 0x00000014 |
| 33 |
uint8_t *polycnt7_table; // length = 127, poly = 0x00000060 |
| 34 |
uint8_t *polycnt9_table; // length = 511, poly = 0x00000110 |
| 35 |
uint8_t *polycnt11_table; // length = 2047, poly = 0x00000500 |
| 36 |
uint8_t *polycnt15_table; // length = 32767, poly = 0x00006000 |
| 37 |
uint8_t *polycnt17_table; // length = 131071, poly = 0x00012000 |
| 38 |
// ---------------- |
| 39 |
DaveTables(); |
| 40 |
~DaveTables(); |
| 41 |
}; |
| 42 |
|
| 43 |
class Dave { |
| 44 |
public: |
| 45 |
DaveTables t; |
| 46 |
int clockDiv; // 2 if bit 1 of port 0xBF is 0, 3 otherwise |
| 47 |
int clockCnt; // counts from 'clockDiv' towards zero |
| 48 |
// variable length counter uses one of the 9, 11, 15, and 17 bit tables |
| 49 |
uint8_t *polycntVL_table; |
| 50 |
// polynomial counters |
| 51 |
int polycnt4_phase; // 4-bit counter phase (14 -> 0) |
| 52 |
int polycnt5_phase; // 5-bit counter phase (30 -> 0) |
| 53 |
int polycnt7_phase; // 7-bit counter phase (126 -> 0) |
| 54 |
int polycntVL_phase; // variable length counter phase ... |
| 55 |
int polycntVL_maxphase; // ... counts from this value to zero |
| 56 |
int polycnt4_state; // 4-bit counter output |
| 57 |
int polycnt5_state; // 5-bit counter output |
| 58 |
int polycnt7_state; // 7-bit counter output |
| 59 |
int polycntVL_state; // variable length counter output |
| 60 |
// fixed frequency counters (f = 250000 / (n + 1)) |
| 61 |
static const int clk_62500_frq = 3; |
| 62 |
static const int clk_1000_frq = 249; |
| 63 |
static const int clk_50_frq = 19; // clocked by the 1 kHz counter |
| 64 |
static const int clk_1_frq = 49; // clocked by the 50 Hz counter |
| 65 |
int clk_62500_phase; |
| 66 |
int clk_1000_phase; |
| 67 |
int clk_50_phase; |
| 68 |
int clk_1_phase; |
| 69 |
int clk_62500_state; |
| 70 |
// channel 0 parameters |
| 71 |
int chn0_state; // current output state |
| 72 |
int chn0_prv; // previous output state |
| 73 |
int chn0_state1; // oscillator output |
| 74 |
int chn0_phase; // phase (frqcode -> 0) |
| 75 |
int chn0_frqcode; // frequency code (0 - 4095) |
| 76 |
int *chn0_input_polycnt; // polynomial counter |
| 77 |
int chn0_hp_1; // enable highpass filter |
| 78 |
int chn0_rm_2; // enable ring modulation |
| 79 |
int chn0_run; // 1: oscillator is running |
| 80 |
int chn0_left; // left volume (0 - 63) |
| 81 |
int chn0_right; // right volume (0 - 63) |
| 82 |
// channel 1 parameters |
| 83 |
int chn1_state; // current output state |
| 84 |
int chn1_prv; // previous output state |
| 85 |
int chn1_state1; // oscillator output |
| 86 |
int chn1_phase; // phase (frqcode -> 0) |
| 87 |
int chn1_frqcode; // frequency code (0 - 4095) |
| 88 |
int *chn1_input_polycnt; // polynomial counter |
| 89 |
int chn1_hp_2; // enable highpass filter |
| 90 |
int chn1_rm_3; // enable ring modulation |
| 91 |
int chn1_run; // 1: oscillator is running |
| 92 |
int chn1_left; // left volume (0 - 63) |
| 93 |
int chn1_right; // right volume (0 - 63) |
| 94 |
// channel 2 parameters |
| 95 |
int chn2_state; // current output state |
| 96 |
int chn2_prv; // previous output state |
| 97 |
int chn2_state1; // oscillator output |
| 98 |
int chn2_phase; // phase (frqcode -> 0) |
| 99 |
int chn2_frqcode; // frequency code (0 - 4095) |
| 100 |
int *chn2_input_polycnt; // polynomial counter |
| 101 |
int chn2_hp_3; // enable highpass filter |
| 102 |
int chn2_rm_0; // enable ring modulation |
| 103 |
int chn2_run; // 1: oscillator is running |
| 104 |
int chn2_left; // left volume (0 - 63) |
| 105 |
int chn2_right; // right volume (0 - 63) |
| 106 |
// channel 3 (noise) parameters |
| 107 |
int chn3_state; // current output state |
| 108 |
int chn3_prv; // previous output state |
| 109 |
int chn3_state1; // polynomial counter output |
| 110 |
int chn3_state2; // lowpass filter output |
| 111 |
int *chn3_clk_source; // clock input signal |
| 112 |
int chn3_clk_source_prv; // previous clock input |
| 113 |
int noise_polycnt_is_7bit; // 0xA6 port bit 4 |
| 114 |
int *chn3_input_polycnt; // polynomial counter |
| 115 |
int chn3_lp_2; // enable lowpass filter |
| 116 |
int chn3_hp_0; // enable highpass filter |
| 117 |
int chn3_rm_1; // enable ring modulation |
| 118 |
int chn3_left; // left volume (0 - 63) |
| 119 |
int chn3_right; // right volume (0 - 63) |
| 120 |
// enable DAC mode for left/right channel |
| 121 |
int dac_mode_left; |
| 122 |
int dac_mode_right; |
| 123 |
// interrupts |
| 124 |
int *int_snd_phase; |
| 125 |
int enable_int_snd; |
| 126 |
int enable_int_1hz; |
| 127 |
int enable_int_1; |
| 128 |
int enable_int_2; |
| 129 |
int int_snd_state; |
| 130 |
int int_1hz_state; |
| 131 |
int int_1_state; |
| 132 |
int int_2_state; |
| 133 |
int int_snd_active; |
| 134 |
int int_1hz_active; |
| 135 |
int int_1_active; |
| 136 |
int int_2_active; |
| 137 |
uint32_t audioOutput; |
| 138 |
uint8_t page0Segment; |
| 139 |
uint8_t page1Segment; |
| 140 |
uint8_t page2Segment; |
| 141 |
uint8_t page3Segment; |
| 142 |
int tape_feedback; |
| 143 |
int tape_input; |
| 144 |
int tape_input_level; |
| 145 |
int keyboardRow; |
| 146 |
uint8_t keyboardState[16]; |
| 147 |
// b0..b3 = current nibble |
| 148 |
// b4 = button 1 (left) state (0 = pressed) |
| 149 |
// b5 = button 2 (right) state |
| 150 |
uint8_t mouseInput; // b7 == 1: mouse data bits inactive |
| 151 |
// ---------------- |
| 152 |
inline void triggerIntSnd(); |
| 153 |
inline void triggerInt1Hz(); |
| 154 |
int * findPolycntForToneChannel(int n); |
| 155 |
uint32_t runOneCycle_(); |
| 156 |
public: |
| 157 |
Dave(); |
| 158 |
virtual ~Dave(); |
| 159 |
protected: |
| 160 |
virtual void setMemoryPage(uint8_t page, uint8_t segment); |
| 161 |
virtual void setMemoryWaitMode(int mode); |
| 162 |
virtual void setRemote1State(int state); |
| 163 |
virtual void setRemote2State(int state); |
| 164 |
virtual void interruptRequest(); |
| 165 |
virtual void clearInterruptRequest(); |
| 166 |
public: |
| 167 |
/*! |
| 168 |
* Run DAVE emulation for 2 us (clock frequency = 500 kHz). |
| 169 |
* Return value is audio output in left_channel + (right_channel << 16) |
| 170 |
* format, where the range for a single channel is 0 to 40320 (sum of 4 |
| 171 |
* sound generators and tape feedback, 0 to 8064 each). |
| 172 |
*/ |
| 173 |
inline uint32_t runOneCycle() |
| 174 |
{ |
| 175 |
if (--clockCnt > 0) |
| 176 |
return audioOutput; |
| 177 |
clockCnt = clockDiv; |
| 178 |
return runOneCycle_(); |
| 179 |
} |
| 180 |
/*! |
| 181 |
* Write to a DAVE register. |
| 182 |
*/ |
| 183 |
void writePort(uint16_t addr, uint8_t value); |
| 184 |
/*! |
| 185 |
* Read from a DAVE register. |
| 186 |
*/ |
| 187 |
uint8_t readPort(uint16_t addr) const; |
| 188 |
/*! |
| 189 |
* Set hardware interrupt 1 state, and (possibly) trigger interrupt. |
| 190 |
*/ |
| 191 |
void setInt1State(int new_state); |
| 192 |
/*! |
| 193 |
* Set hardware interrupt 2 state, and (possibly) trigger interrupt. |
| 194 |
*/ |
| 195 |
void setInt2State(int new_state); |
| 196 |
/*! |
| 197 |
* Set tape input state, and level (0: low, 1: high). |
| 198 |
*/ |
| 199 |
void setTapeInput(int state, int level); |
| 200 |
/*! |
| 201 |
* Set state of key 'keyCode' (0 to 127, see table below) to pressed |
| 202 |
* (state != 0) or released (state == 0). |
| 203 |
* +-------+-------+-------+-------+-------+-------+-------+-------+ |
| 204 |
* | 0x00 | 0x01 | 0x02 | 0x03 | 0x04 | 0x05 | 0x06 | 0x07 | |
| 205 |
* | 0x08 | 0x09 | 0x0A | 0x0B | 0x0C | 0x0D | 0x0E | 0x0F | |
| 206 |
* +------+-------+-------+-------+-------+-------+-------+-------+-------+ |
| 207 |
* | 0x00 | N | \ | B | C | V | X | Z | SHF_L | |
| 208 |
* +------+-------+-------+-------+-------+-------+-------+-------+-------+ |
| 209 |
* | 0x08 | H | LOCK | G | D | F | S | A | CTRL | |
| 210 |
* +------+-------+-------+-------+-------+-------+-------+-------+-------+ |
| 211 |
* | 0x10 | U | Q | Y | R | T | E | W | TAB | |
| 212 |
* +------+-------+-------+-------+-------+-------+-------+-------+-------+ |
| 213 |
* | 0x18 | 7 | 1 | 6 | 4 | 5 | 3 | 2 | ESC | |
| 214 |
* +------+-------+-------+-------+-------+-------+-------+-------+-------+ |
| 215 |
* | 0x20 | F4 | F8 | F3 | F6 | F5 | F7 | F2 | F1 | |
| 216 |
* +------+-------+-------+-------+-------+-------+-------+-------+-------+ |
| 217 |
* | 0x28 | 8 | | 9 | - | 0 | ^ | ERASE | | |
| 218 |
* +------+-------+-------+-------+-------+-------+-------+-------+-------+ |
| 219 |
* | 0x30 | J | | K | ; | L | : | ] | | |
| 220 |
* +------+-------+-------+-------+-------+-------+-------+-------+-------+ |
| 221 |
* | 0x38 | STOP | DOWN | RIGHT | UP | HOLD | LEFT | ENTER | ALT | |
| 222 |
* +------+-------+-------+-------+-------+-------+-------+-------+-------+ |
| 223 |
* | 0x40 | M | DEL | , | / | . | SHF_R | SPACE | INS | |
| 224 |
* +------+-------+-------+-------+-------+-------+-------+-------+-------+ |
| 225 |
* | 0x48 | I | | O | @ | P | [ | | | |
| 226 |
* +------+-------+-------+-------+-------+-------+-------+-------+-------+ |
| 227 |
* | 0x70 | JOY1R | JOY1L | JOY1D | JOY1U | JOY1F | JOY1F2| JOY1F3| | |
| 228 |
* +------+-------+-------+-------+-------+-------+-------+-------+-------+ |
| 229 |
* | 0x78 | JOY2R | JOY2L | JOY2D | JOY2U | JOY2F | JOY2F2| JOY2F3| | |
| 230 |
* +------+-------+-------+-------+-------+-------+-------+-------+-------+ |
| 231 |
*/ |
| 232 |
void setKeyboardState(int keyCode, int state); |
| 233 |
inline void setMouseInput(uint8_t value) |
| 234 |
{ |
| 235 |
mouseInput = value; |
| 236 |
} |
| 237 |
inline void clearMouseInput() |
| 238 |
{ |
| 239 |
// clear data bits, but not the buttons |
| 240 |
mouseInput = mouseInput | 0xCF; |
| 241 |
} |
| 242 |
/*! |
| 243 |
* Reset DAVE. |
| 244 |
*/ |
| 245 |
void reset(bool isColdReset = false); |
| 246 |
}; |
| 247 |
|
| 248 |
} // namespace Ep128 |
| 249 |
|
| 250 |
#endif // EP128EMU_DAVE_HPP |
| 251 |
|