src/mess/audio/upd1771.c
| r7108 | r7109 | |
| 2 | 2 | |
| 3 | 3 | NEC uPD1771 |
| 4 | 4 | |
| 5 | Made using recording/analysis on a Yeno (PAL Super Cassete Vision) |
| 6 | by plgDavid |
| 7 | |
| 5 | 8 | **********************************************************************/ |
| 6 | 9 | |
| 7 | 10 | #include "emu.h" |
| r7108 | r7109 | |
| 11 | 14 | |
| 12 | 15 | #define LOG 1 |
| 13 | 16 | |
| 14 | | #define MAX_PACKET_SIZE 1 |
| 17 | #define MAX_PACKET_SIZE 16 //protection/padding max is really 1 |
| 15 | 18 | |
| 16 | 19 | |
| 20 | |
| 21 | /* |
| 22 | Each of the 32 waveform has been sampled at 192Khz, filtered, and each level has been |
| 23 | calculated with averages on around 10 samples (removing the transition samples) |
| 24 | then quantized to 8bit signed. (not clear on the exact DAC details yet) |
| 25 | */ |
| 26 | const char WAVEFORMS[8][32]={ |
| 27 | |
| 28 | {-5,-5,-5,-117,-116,-53,-10,127,120,108,97,-121,-121,-121,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-119,-119,-118,-2,-2,-2,-2,-2}, |
| 29 | {6,-21,-1,-41,-1,25,-35,-35,-1,-16,34,29,-37,-30,-33,-20,38,-15,50,-20,-20,-15,7,-20,77,-15,-37,69,93,-21,-38,-37}, |
| 30 | {-11,-4,-11,51,-9,-11,-11,84,87,-112,44,102,-86,-112,35,103,-12,51,-10,-12,-12,-9,-12,13,-11,-44,25,103,-12,-5,-90,-101}, |
| 31 | {40,98,31,98,-1,13,58,3,-18,45,-5,-13,-5,-13,-5,-13,-5,-13,-5,-13,-10,-15,-121,5,-17,45,-128,8,-16,-12,-16,-9}, |
| 32 | {-53,-101,-121,-128,-113,-77,-34,5,26,63,97,117,119,119,115,99,54,13,-13,-11,-2,3,31,52,62,74,60,51,38,22,8,-14}, |
| 33 | {-86,-128,-60,3,65,101,119,44,37,41,51,53,55,58,58,29,-12,74,82,77,59,113,52,21,24,34,39,45,48,48,48,-13}, |
| 34 | {-15,-18,-46,-67,-95,-111,-117,-124,-128,-123,-116,-105,-89,-72,-50,-21,2,16,46,76,95,111,118,119,119,119,117,110,97,75,47,18}, |
| 35 | {-84,-121,-128,-105,-51,7,38,66,93,97,93,88,89,96,102,111,116,118,118,119,118,118,117,117,118,118,117,117,117,115,85,-14} |
| 36 | |
| 37 | }; |
| 38 | |
| 39 | #define STATE_SILENCE 0 |
| 40 | #define STATE_NOISE 1 |
| 41 | #define STATE_TONE 2 |
| 42 | |
| 17 | 43 | typedef struct _upd1771_state upd1771_state; |
| 18 | 44 | struct _upd1771_state |
| 19 | 45 | { |
| r7108 | r7109 | |
| 24 | 50 | UINT8 packet[MAX_PACKET_SIZE]; |
| 25 | 51 | UINT8 index; |
| 26 | 52 | UINT8 expected_bytes; |
| 53 | |
| 54 | UINT8 state;//0:silence, 1 noise, 2 tone |
| 55 | |
| 56 | //tone |
| 57 | UINT8 t_timbre; //[0; 7] |
| 58 | UINT8 t_offset; //[0; 32] |
| 59 | UINT16 t_period; //[0;255] |
| 60 | UINT8 t_volume; //[0; 31] |
| 61 | |
| 62 | UINT8 t_tpos;//timber pos |
| 63 | UINT16 t_ppos;//period pos |
| 27 | 64 | }; |
| 28 | 65 | |
| 29 | 66 | |
| r7108 | r7109 | |
| 37 | 74 | } |
| 38 | 75 | |
| 39 | 76 | |
| 77 | /* |
| 78 | *************TONE***************** |
| 79 | Tone consist of a wavetable playback mechanism. |
| 80 | Each wavetable is a looping period of 32 samples but can be played with an offset from any point in the table |
| 81 | effectively shrinking the sample loop, thus allowing different pitch "macros ranges" to be played. |
| 82 | This method is rather crude because the spectrum of the sound get heavily altered... |
| 83 | unless that was the intent. |
| 84 | |
| 85 | Tone Write (4 bytes): |
| 86 | |
| 87 | Byte0: 0x02 |
| 88 | |
| 89 | Byte1: 0bTTTOOOOO |
| 90 | MSB 3 bits of Timbre (8 wavetables) |
| 91 | LSB 5 bits offset in the table. |
| 92 | |
| 93 | Byte2: 0bPPPPPPPP |
| 94 | 8bits of clock divider/period |
| 95 | Anything under <= 0x20 give the same value |
| 96 | |
| 97 | Byte3: 0b???VVVVV |
| 98 | MSB 3 bits unknown |
| 99 | LSB 5 bits of "Volume" |
| 100 | |
| 101 | Note: volume is not a volume in a normal sense but some kind |
| 102 | of bit cropping/rounding. |
| 103 | */ |
| 104 | |
| 105 | |
| 106 | |
| 40 | 107 | WRITE8_DEVICE_HANDLER( upd1771_w ) |
| 41 | 108 | { |
| 42 | 109 | upd1771_state *state = get_safe_token( device ); |
| 43 | 110 | |
| 44 | | if (LOG) |
| 45 | | logerror( "upd1771_w: received byte 0x%02x\n", data ); |
| 111 | //if (LOG) |
| 112 | // logerror( "upd1771_w: received byte 0x%02x\n", data ); |
| 46 | 113 | |
| 47 | | state->packet[ state->index ] = data; |
| 48 | | state->index = state->index + 1; |
| 49 | | |
| 50 | 114 | devcb_call_write_line( &state->ack_out_func, 0 ); |
| 51 | 115 | |
| 52 | | /* Start of a new command */ |
| 53 | | if ( state->expected_bytes == 0 ) |
| 54 | | { |
| 55 | | switch ( data ) |
| 116 | state->packet[state->index++]=data; |
| 117 | |
| 118 | switch(state->packet[0]){ |
| 119 | |
| 120 | case 0: |
| 56 | 121 | { |
| 57 | | case 0x00: /* 00 - 1 byte command */ |
| 58 | | state->expected_bytes = 1; |
| 59 | | break; |
| 60 | | case 0x01: /* 01 - 10 byte command */ |
| 61 | | state->expected_bytes = 10; |
| 62 | | case 0x02: /* 02 - 4 byte command */ |
| 63 | | state->expected_bytes = 4; |
| 64 | | break; |
| 122 | state->state = STATE_SILENCE; |
| 123 | state->index = 0; |
| 124 | //logerror( "upd1771_w: ----------------silence state reset\n"); |
| 125 | }break; |
| 126 | |
| 127 | case 1: |
| 128 | if (state->index == 10){ |
| 129 | state->state = STATE_NOISE; //TODO |
| 130 | state->index = 0; |
| 131 | //logerror( "upd1771_w: ----------------noise state reset\n"); |
| 132 | } |
| 133 | else |
| 134 | timer_adjust_oneshot( state->timer, ticks_to_attotime( 512, device->clock ), 0 ); |
| 135 | break; |
| 136 | |
| 137 | case 2: |
| 138 | if (state->index == 4){ |
| 139 | //logerror( "upd1771_w: ----------------tone state reset\n"); |
| 140 | state->t_timbre = (state->packet[1] & 0xE0) >> 5; |
| 141 | state->t_offset = (state->packet[1] & 0x1F); |
| 142 | state->t_period = state->packet[2]; |
| 143 | //smaller periods dont all equal to 0x20 |
| 144 | if (state->t_period < 0x20) |
| 145 | state->t_period = 0x20; |
| 146 | |
| 147 | state->t_volume = state->packet[3] & 0x1f; |
| 148 | state->state = STATE_TONE; |
| 149 | state->index = 0; |
| 150 | } |
| 151 | else |
| 152 | timer_adjust_oneshot( state->timer, ticks_to_attotime( 512, device->clock ), 0 ); |
| 153 | |
| 154 | break; |
| 155 | |
| 156 | //garbage: wipe stack |
| 65 | 157 | default: |
| 66 | | if (LOG) |
| 67 | | logerror("upd1771: unknown command %02x received\n", data ); |
| 68 | | state->expected_bytes = 99; |
| 69 | | break; |
| 70 | | } |
| 158 | state->index = 0; |
| 159 | break; |
| 71 | 160 | } |
| 72 | | |
| 73 | | if ( state->index == state->expected_bytes ) |
| 74 | | { |
| 75 | | /* The command packet has been transferred, handle the command */ |
| 76 | | if (LOG) |
| 77 | | logerror("upd1771_w: handle command %02x\n", state->packet[0] ); |
| 78 | | state->expected_bytes = 0; |
| 79 | | } |
| 80 | | else |
| 81 | | { |
| 82 | | /* We need more data to complete the command packet */ |
| 83 | | /* Trigger ACK after 512??? cycles */ |
| 84 | | timer_adjust_oneshot( state->timer, ticks_to_attotime( 512, device->clock ), 0 ); |
| 85 | | } |
| 86 | | |
| 87 | | state->index = state->index % MAX_PACKET_SIZE; |
| 88 | 161 | } |
| 89 | 162 | |
| 90 | 163 | |
| 91 | 164 | static STREAM_UPDATE( upd1771c_update ) |
| 92 | 165 | { |
| 93 | | // upd1771_state *state = (upd1771_state *)param; |
| 166 | upd1771_state *state = get_safe_token( device ); |
| 94 | 167 | stream_sample_t *buffer = outputs[0]; |
| 95 | 168 | |
| 96 | | while ( samples > 0 ) |
| 97 | | { |
| 98 | | *buffer = 0; |
| 99 | | buffer++; |
| 100 | | samples--; |
| 169 | switch(state->state){ |
| 170 | case STATE_TONE: |
| 171 | { |
| 172 | //logerror( "upd1771_STATE_TONE samps:%d %d %d %d %d %d\n",(int)samples, |
| 173 | // (int)state->t_timbre,(int)state->t_offset,(int)state->t_volume,(int)state->t_period,(int)state->t_tpos); |
| 174 | |
| 175 | while ( samples > 0 ){ |
| 176 | |
| 177 | *buffer = (WAVEFORMS[state->t_timbre][state->t_tpos])*state->t_volume * 2; |
| 178 | |
| 179 | state->t_ppos++; |
| 180 | if (state->t_ppos >= state->t_period){ |
| 181 | |
| 182 | state->t_tpos++; |
| 183 | if (state->t_tpos == 32) |
| 184 | state->t_tpos = state->t_offset; |
| 185 | |
| 186 | state->t_ppos = 0; |
| 187 | } |
| 188 | |
| 189 | buffer++; |
| 190 | samples--; |
| 191 | } |
| 192 | }break; |
| 193 | |
| 194 | default: |
| 195 | { |
| 196 | //fill buffer with silence |
| 197 | while ( samples > 0 ) |
| 198 | { |
| 199 | *buffer = 0; |
| 200 | buffer++; |
| 201 | samples--; |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | break; |
| 101 | 206 | } |
| 207 | |
| 102 | 208 | } |
| 103 | 209 | |
| 104 | 210 | |
| r7108 | r7109 | |
| 117 | 223 | upd1771_state *state = get_safe_token( device ); |
| 118 | 224 | int sample_rate = device->clock / 4; |
| 119 | 225 | |
| 226 | |
| 120 | 227 | /* resolve callbacks */ |
| 121 | 228 | devcb_resolve_write_line( &state->ack_out_func, &intf->ack_callback, device ); |
| 122 | 229 | |
| r7108 | r7109 | |
| 139 | 246 | } |
| 140 | 247 | |
| 141 | 248 | |
| 249 | static DEVICE_STOP( upd1771c ) |
| 250 | { |
| 251 | } |
| 252 | |
| 253 | |
| 142 | 254 | DEVICE_GET_INFO( upd1771c ) |
| 143 | 255 | { |
| 144 | 256 | switch (state) |
| r7108 | r7109 | |
| 149 | 261 | /* --- the following bits of info are returned as pointers to functions --- */ |
| 150 | 262 | case DEVINFO_FCT_START: info->start = DEVICE_START_NAME( upd1771c ); break; |
| 151 | 263 | case DEVINFO_FCT_RESET: info->reset = DEVICE_RESET_NAME( upd1771c ); break; |
| 264 | case DEVINFO_FCT_STOP: info->stop = DEVICE_STOP_NAME( upd1771c ); break; |
| 152 | 265 | |
| 266 | |
| 153 | 267 | /* --- the following bits of info are returned as NULL-terminated strings --- */ |
| 154 | | case DEVINFO_STR_NAME: strcpy(info->s, "NEC uPD1771C"); break; |
| 268 | case DEVINFO_STR_NAME: strcpy(info->s, "NEC uPD1771C 017"); break; |
| 155 | 269 | case DEVINFO_STR_FAMILY: strcpy(info->s, "NEC uPD1771"); break; |
| 156 | 270 | case DEVINFO_STR_VERSION: strcpy(info->s, "1.0"); break; |
| 157 | 271 | case DEVINFO_STR_SOURCE_FILE: strcpy(info->s, __FILE__); break; |