|
Having explored how an AVR can be used to emulate the AY-3-8912/8910 in AY-3-8912/8910 Hardware Emulation I wanted to have a go at using the very common ATMega328P to do the same. But rather than wire things up on breadboard, I put together a quick PCB to allow me to do some experiments. |
Spoilers: This isn’t working yet!! Read on for where I’ve got to, but I’ll need to come back to this at some point. |
Warning! I strongly recommend using old or second hand equipment for your experiments. I am not responsible for any damage to expensive instruments! |
If you are new to electronics and microcontrollers, see the Getting Started pages. |
But with an ATMega328P rather than the suggested ATMega48PA (more about that in part one here: AY-3-8912/8910 Hardware Emulation). I’ve included headers for both the AY-3-8910 and AY-3-8912. |
I’ve kept the same usage of ATMega ports, which map onto IO for the ATMega328P as follows: |
| AY-3-8912 |
Schematic |
ATMega328P PORT |
ATMega328P Pin |
Arduino Equivalent |
| D0-D5 |
DA0-DA5 |
PC0-PC5 |
23-28 |
A0-A5 |
| D6-D7 |
DA6-DA7 |
PD6-PD7 |
12-13 |
D6-D7 |
| A8 |
A8_CIPO |
PB4 |
18 |
D12 |
| BC1 |
BC1 |
PD2 |
4 |
D2 |
| BC2 |
BC2_SCK |
PB5 |
19 |
D13 |
| BDIR |
BDIR |
PD3 |
5 |
D3 |
| CLOCK |
CLOCK |
PD4 |
6 |
D4 |
| RESET |
RESET |
PC6 |
1 |
RESET |
| TEST |
N/C |
|
|
|
| IOA7-IOA0 |
N/C |
|
|
|
| C |
PWM_C |
PB2 |
16 |
D10 |
| B |
PWM_B_COPI |
PB3 |
17 |
D11 |
| A |
PWM_A |
PB1 |
15 |
D9 |
|
A9_CFG0 |
PB0 |
14 |
D8 |
|
CFG1 |
PD5 |
6 |
D5 |
|
RX |
PD0 |
2 |
D0 |
|
TX |
PD1 |
3 |
D1 |
| VCC |
VCC |
VCC |
7, 20 |
VCC |
| GND |
GND |
GND |
8 |
GND |
|
The only change is I’m not connecting TEST to anything. I’ll have to see if this becomes an issue later… |
The circuit calls for overclocking the ATMega328P to 27MHz (again more discussion on this in the first part). |
I’ve included breakout headers for ICSP and a serial header and have swapped the oscillator to a crystal using a common configuration I’ve seen on many DIY Arduino-style boards using an ATMega328P. |
Rather than put together two boards, one for the 8912 and one for the 8910, I’ve also put together a simple adaptor circuit to allow one to plug into the socket for the other. |
The schematic essentially just maps the signals from one footprint onto the other. |
The only slight complication is that /A9 for the 8910 doesn’t exist on the 8912, so I’ve left in a jumper to provide the option of tying this to GND or VCC should the need arise. This should allow the larger 8910 to be used in the place where a 8912 is required whilst still enabling the 8910. |
I did wonder quite how much of a 28 pin DIP could fit inside the footprint of a 28 pin WDIP and still keep through hole components, but I didn’t have to wonder for long to see it wasn’t really going to work. |
So as this is basically just for messing around I went with the format as shown. This way, round-profile pin headers can be used on the underside for the AY footprint if the board is to replace a genuine device. Alternatively normal pin headers or sockets can be used on the topside if jumper wires are going to be used to hook this board into another PCB. |
The two CFG jumpers are solder bridges (default not connected) on the underside of the board. I’ve also listed the Arduino versions of the AVR pins used on the underside too. |
For the converter board, I’ve left the /A9 jumper as standard pin headers. With hindsight I’m not sure when this would be wanted to be tied high, as presumably that would disable the chip. When plugging a 8912 into a 8910 socket, anything expecting an 8910 and using /A9 as part of the addressing scheme would require some additional logic before mapping it onto the smaller 8912. |
- AVR-AY-Board PCB (GitHub link below).
- ATMega329P (28 pin DIP version).
- 1x 1K resistor
- 3x 3K6 resistors
- 1x 10K resistor
- 1x 10uF electrolytic capacitor
- 2x 100nF ceramic capacitors
- 3x 2.2nF ceramic capacitors
- 2x 18pF ceramic capacitors
- 1x 3mm LED (colour to taste)
- 1x 27 MHz crystal (2 pin, low-profile, HC-49 package – see footprints)
- 1x 2-pin momentary push switch (see footprints)
- Optional: 28-way narrow DIP socket
- Optional: round-pin header pins (see photos and discussion)
- Range of pin headers or sockets as required
AY-3-8910 to 12 Converter: |
- AY-3-8910-12 Converter PCB (GitHub link below).
- Either 40-pin WDIP socket or 28-pin WDIP socket.
- Round-pin header pins.
AY-3-8910 to 12 Converter Build |
This is a relatively straight forward build, but depending on the DIP sockets used it might be necessary to doctor the socket slightly to sit neatly over the soldered pins. |
- Round pins for either the 28 pin or 40 pin part of the PCB.
- The 40 pin or 28 pin DIP socket.
If the converter is from a 8912 socket to a 8910 device, then the /A9 jumper or a solder link will have to be configured, presumably connecting it to GND to make it permanently active. |
One note of caution – the pins I was using are not particularly robust, so when removing the adaptor from its socket, I managed to break one and had to replace it, which wasn’t easy. |
But apart from that, it works! |
As already mentioned there are a number of configuration options for the 28-pin AY-3-8910 emulation as illustrated below. |
The first two are designed for jumper wire connections. The third shows the use of pins which would allow the board, space permitting, to be inserted into a AY-3-8910 socket on an existing board. |
These headers will be soldered on last, but it is worth deciding in advance what configuration will be required. |
I’ve chosen not to populate the UART and ICSP headers, and am using round pins so that this can hopefully replace an AY-3-8912. But at the moment all my test boards are built for the AY-3-8910, so I’m also having to use the converter, which has led to the following stack of boards! |
The stand-alone ATMega328P chip cannot be programmed directly from the Arduino IDE with this code as it stands. Instead the following are required: |
- AVRDude: https://github.com/avrdudes/avrdude
- An AVR Programmer: I’m using a cheap USBasp clone, so one based on this: https://github.com/piit79/USBasp
- Connection to the device via ICSP: I’m using a homemade link, but it should be possible to use the ICSP header on the PCB; plug it into a DIP-version of another Arduino Uno board; or possibly even a second Arduino Uno board in “Arduino as ISP” mode.
Update: I ended up using a slightly different firmware and configuration – see later… |
I’m using v1.0 of the firmware and have downloaded the binary firmware and the 1.75MHz configuration. I now have the following files: |
C:\Kevin\Temp> dir 09/05/2026 15:49 <DIR> . 09/05/2026 15:49 <DIR> .. 09/05/2026 15:49 1,920 avr-psg.hex 09/05/2026 15:40 909,660 avrdude.conf 09/05/2026 15:40 9,604,608 avrdude.exe 09/05/2026 15:40 13,316,096 avrdude.pdb 09/05/2026 15:49 34 config-1.75mhz.hex C:\Kevin\Temp> |
To program the ATMega328P requires the following instructions to get the code into flash, the configuration into EEPROM and to set the fuses for the MCU: |
C:\Kevin\Temp>avrdude -c USBasp -p ATMega328P -U flash:w:avr-psg.hex:i Error: cannot set sck period; please check for usbasp firmware update Error: cannot set sck period; please check for usbasp firmware update Reading 682 bytes for flash from input file avr-psg.hex Writing 682 bytes to flash Writing | ################################################## | 100% 0.43 s Reading | ################################################## | 100% 0.23 s 682 bytes of flash verified
Avrdude done. Thank you.
C:\Kevin\Temp>avrdude -c USBasp -p ATMega328P -U eeprom:w:config-1.75mhz.hex:i Error: cannot set sck period; please check for usbasp firmware update Reading 5 bytes for eeprom from input file config-1.75mhz.hex Writing 5 bytes to eeprom Writing | ################################################## | 100% 0.09 s Reading | ################################################## | 100% 0.01 s 5 bytes of eeprom verified
Avrdude done. Thank you.
C:\Kevin\Test>avrdude -c USBasp -p ATMega328P -U lfuse:w:0xdf:m -U hfuse:w:0xdf:m -U efuse:w:0xfd:m Error: cannot set sck period; please check for usbasp firmware update
Processing -U lfuse:w:0xdf:m Reading 1 byte for lfuse from input file 0xdf Writing 1 byte (0xDF) to lfuse, 1 byte written, 1 verified
Processing -U hfuse:w:0xdf:m Reading 1 byte for hfuse from input file 0xdf Writing 1 byte (0xDF) to hfuse, 1 byte written, 1 verified
Processing -U efuse:w:0xfd:m Reading 1 byte for efuse from input file 0xfd Writing 1 byte (0xFD) to efuse, 1 byte written, 1 verified
Avrdude done. Thank you.
|
The error “cannot set sck period” is apparently pretty common especially with USBasp clones, and can be ignored. |
The three fuse settings come from the original AVR-AY firmware (here) in the readme as follows: |
ATMEGA328 =================================================== avrdude -p m328p -c USBasp -U flash:w:AY_Emul_XXX_Nch_KK_MM.hex -U eeprom:w:Conf_XXX_YYMHz_ZZMhz.hex -U lfuse:w:0xee:m -U hfuse:w:0xdf:m -U efuse:w:0xfd:m Example: avrdude -p m328p -c USBasp -U flash:w:AY_Emul_250_2ch_m328_ay.hex -U eeprom:w:Conf_standard_27MHz_1_75Mhz.hex -U lfuse:w:0xee:m -U hfuse:w:0xdf:m -U efuse:w:0xfd:m ============================================================= |
0xEE = b1110 1110 CKDIV8 = 1 (unprogrammed) CLKOUT = 1 (unprogrammed) SUT = 10 CLKSEL = 1110 |
Which from the AVR fusecalc site maps onto: |
- External crystal oscillator
- Frequency 8MHz +
- Start up time 1K
- Powerdown/reset 16
However, following through the settings in the datasheet, it is similar but slightly different, giving: |
- Low power crystal oscillator (CLKSEL3:0 = 1111 – 1000)
- Low power operating mode 111 = 8.0-16.0 MHz (with recommended caps 12-22pF)
- CLKSEL0+SUT = 0+10 = Ceramic Resonator BOD enabled, Start up time 1KCK, additional delay 14CK
But I’m not using a ceramic resonator, I’m using a crystal oscillator, so I think I need the mode: |
- Crystal oscillator BOD enabled, startup time 16KCK, additional delay 14CK = CLKSEL0+SUT = 1+01
There is no “1KCK” for the crystal oscillator settings, so I’ll have to use 16KCK I guess? |
This is what gives me the 0xDF value I’ve used: |
0xDF = b1101 1111 CKDIV8 = 1 (unprogrammed) CLKOUT = 1 (unprogrammed) SUT = 01 CLKSEL = 1111 |
Actually, this wasn’t working with either 0xEE or 0xDF, so I decide to try to turn off BOD and went with “slow rising power” (CLKSEL0+SUT = 1+11) which gave me the fuses: |
lfuse = b1111 1111 = 0xFF hfuse = 0xDF (as before) efuse = 0xFF (no brown-out detection) |
But that isn’t working either… |
The AY891x Library and BCn/BCDIR control |
Eventually, I remembered reading about the control signals with the library that was designed for use with that test board. It states the following (here): |
“While the PSG has tight timing requirements, it is possible to use digitalWrite() by using all three bus control signals (BDIR, BC1, BC2) and cycling through an extra state when reading and writing the chip registers.” |
But the emulation is ignoring BC2 (more here), relying instead on the ATMega’s two IO interrupt pins INT0 and INT1 on D2 and D3, to trigger on BDIR and BC1. This means I had to ditch the library and instead switched over to my own PORT IO driver as described here: Arduino and AY-3-8910 – Part 3. |
Resetting the fuses as before, unfortunately it still isn’t working. |
At this point I loaded on a simple tone() function on one of the IO pins using the Arduino IDE then set the fuses again as above. I can definitely see that the frequency for the tone() output is almost twice as high as when the ATMega328P is plugged into the Arduino, so the 27MHz crystal is certainly having an effect compared to the Uno’s original 16MHz. |
I can also see the 105kHz PWM carrier frequency on all three PWM output pins, but the peaks are so narrow, these must be responding to a level 0 output which implies all code is working, but there are simply no register writes getting through. |
So, in summary, I think the code on the emulator is running fine – so why isn’t it responding to register writes? |
At this point I left things for a bit and came back to it all a few days later. |
I was chewing over why the register writes didn’t seem to be getting through, so at this point I think the handling of BC1 and BDIR are key. The next things that I tried: |
- Getting it all on a breadboard and showing it working with a real AY-3-8910.
- Check I can see BC1 and BDIR and the data lines doing something.
- Replace the real chip with the emulator.
- Repeat the tests for BC1, BDIR and data – all appear ok.
At this point I’m suspecting the interrupts aren’t getting through (remember BC1 and BDIR are triggered off the external interrupts), so I’m contemplating two things: |
- Some test code that lights an LED on an external interrupt.
- Porting the whole code across to the Arduino environment to let me build and run it all from there.
But this has got me wondering about build differences for the two devices – what might be different between the ATMega48 and ATMega328P. Would rebuilding from source for the ATMega328P sort it out? |
Looking at the original code, I can see there are many different binaries for the different chips. For the repository I’m using there is just one. |
Looking in the two chip datasheets, I can see that the INT0 interrupt vector is different – it is 2 (address 0x001) for the 48 and whist it is still 2 for the 328P, the address is 0x0002. From the 48 datasheet: |
“Each interrupt vector occupies two instruction words in ATmega168, and one instruction word in ATmega48 and
ATmega88.” |
“Each interrupt vector occupies two instruction words in Atmel ATmega328P.” |
Looking at the suggested implementations of the vector tables, they are shown as follows: |
// ATMega48 vector table 0x000 rjmp RESET ; Reset Handler 0x001 rjmp EXT_INT0 ; IRQ0 Handler 0x002 rjmp EXT_INT1 ; IRQ1 Handler
// ATMega328P vector table 0x0000 jmp RESET ; Reset Handler 0x0002 jmp EXT_INT0 ; IRQ0 Handler 0x0004 jmp EXT_INT1 ; IRQ1 Handler |
So presumably one is a relative jump (rjmp) which is a single word (16-bit) instruction, vs an absolute jump which is a two-word (32-bit) instruction. |
With hindsight is seems obvious I’d need to rebuild for the ATMega328P, but the initial talk of compatibility lulled me into a false sense of security! |
There is no build for the 328P in the listed repository, but going back to the original avr-ay source, there are builds for all supported MCUs, each with variants covering: |
- 2 or 3 channel sound.
- AY or YM volume tables.
- Additional speaker output on PD1.
There are configuration options for: |
- Serial, parallel, or both (“standard”).
- 1.75MHz, 1.78MHz, or 2.0MHz operation.
- 20MHz to 40MHz (including my required 27MHz).
avrdude -c USBasp -p ATMega328P -U flash:w:AY_Emul_260_3ch_m328_ay.hex:i avrdude -c USBasp -p ATMega328P -U eeprom:w:Conf_parallel_27MHz_1_78Mhz.hex:i avrdude -c USBasp -p ATMega328P -U lfuse:w:0xee:m -Uhfuse:w:0xdf:m -U efuse:w:0xfd:m |
But unfortunately it still isn’t working… |
I did notice in the original source, a number of conditional assembler code, based on the initial configuration of MCU_TYPE. But most of it is of the form “if MCU_TYPE==0” which is giving alternative code for the ATMega8. There is one piece of code that implies something different for the ATMega48: |
; get byte 0 from EEPROM, check value > 0 or skip USART initialization if value = 0 #if MCU_TYPE == 0 || MCU_TYPE > 1 out EEARH,C00 ; is absent in Atmega48
|
|
|
|
|
No comments:
Post a Comment