Skip to main content
Elektraglide
Associate II
October 26, 2025
Question

Trying to make a PIA tester with a STM32F103

  • October 26, 2025
  • 4 replies
  • 740 views

I'm building a little tester for 6821 PIA using a STM32F103.

I need to be able to write to the data bus of the PIA, and also read from it.  (6821 has 4 registers that are addressed using 2-bits input RS0,RS1).

So 8 GPIO pins for the databus.  And I use 3 more GPIO pins for RS0,RS1 and R/W pin.

For writing to the PIA:
I set the GPIO pins to output,  set the output pins actual data and then make R/W low for writing.

For read from the PIA:
I set the GPIO pins to input,  make R/W high for reading and then read the input pins..


Does that sound right?   I'm concerned about the timing between the GPIO data bus pins switching from output to input (and back)  and then toggling the R/W line to the PIA. 

Is all this switching even needed?  


4 replies

gbm
Principal
October 27, 2025

Considering the timing of STM32F103 and MC6821, everything the STM does is much faster then 6821 response. So, provide all the needed delays and the proper E line timing.

Basically, for writing:

  • set RW LOW
  • set data bus as output
  • set RSx and data value
  • set E high
  • delay
  • set E low
  • set data bus as inputs
  • delay to guarantee the proper timing for E low state

For reading:

  • data lines should be set as inputs - see above
  • set RW high
  • set RSx
  • set E high
  • delay
  • sample data
  • set E low
  • delay

I suspect you might be interested in this:

https://hackaday.io/project/185010-mc6800-computer

 

 

My STM32 stuff on github - compact USB device stack and more: https://github.com/gbm-ii/gbmUSBdevice
Elektraglide
Associate II
October 27, 2025

Nice.  Thank you.

I thought I saw in the 6821 spec sheet it doesn't clock under 150kHz  so I've been clocking it at around that but I do have delays between register writes etc.

And would bit banging the E clock input work?  

 

Ozone
Principal
October 27, 2025

> And would bit banging the E clock input work?  

Probably, assuming the 6821 is a static design.
You would have to check with the datasheet.

I hope you realize the 6821 is a 5V device, you will most probably need some level shifting electronics for the stimulation side.
And either choose 5V-tolerant inputs on the F103 side, or again use level shifting.

gbm
Principal
October 27, 2025

I already forgot that 6821 is not static. In that case, I would rather use timer output in PWM mode for E control and implement the state machine for 6821 access in timer ISR, using Update and CC interrupt. It's hard to achieve the required ISR performance with interrupt routing via HAL, so I'd recommend to write your own, full ISR using direct register access and definitely no HAL for setting the GPIO ports.

To relax timing constraints, instead of using update and CC interrupt from PWM output channel, you may use two other CC interrupts triggered before PWM output changes (close to the end of period and some time before PWM pulse end).

Contrary to what many people say, 300 kHz timer interrupt is not impossible for SMT32F103. I did something around 450 kHz with much more complex service than in your case. Just leave this single interrupt at the highest priority (0) and stay away from HAL for it. (You may still use HAL for configuration and other stuff outside of this ISR.)

I actually did something similar few years ago in my SDC_One MC6800 computer, in which STM32 was both supplying the clock for MC6800 and serving as memory and peripheral device for it.

Here is the link; unfortunately the MC6800 version is not mentioned and no photo is shown.

https://hackaday.io/project/164007-sdcone

My STM32 stuff on github - compact USB device stack and more: https://github.com/gbm-ii/gbmUSBdevice
Elektraglide
Associate II
October 27, 2025

@gbm wrote:

I already forgot that 6821 is not static. In that case, I would rather use timer output in PWM mode for E control and implement the state machine for 6821 access in timer ISR, using Update and CC interrupt. It's hard to achieve the required ISR performance with interrupt routing via HAL, so I'd recommend to write your own, full ISR using direct register access and definitely no HAL for setting the GPIO ports.

To relax timing constraints, instead of using update and CC interrupt from PWM output channel, you may use two other CC interrupts triggered before PWM output changes (close to the end of period and some time before PWM pulse end).

Contrary to what many people say, 300 kHz timer interrupt is not impossible for SMT32F103. I did something around 450 kHz with much more complex service than in your case. Just leave this single interrupt at the highest priority (0) and stay away from HAL for it. (You may still use HAL for configuration and other stuff outside of this ISR.)

 


Did exactly that and have TIM4 use AF to waggle PB9 @ 1MHz

And yes, I found I could do around 400kHz with a TIM4 interrupt by hitting registers directly:

if (TIM4->SR & TIM_IT_Update)
{
 TIM4->SR = ~TIM_IT_Update;

 TimeSec++;
 if ((TimeSec & 1) == 0)
 GPIOC->BSRR = GPIO_Pin_13;
 else
 GPIOC->BRR = GPIO_Pin_13;
}
 
gbm
Principal
October 27, 2025

From my experience, the way to go is to use hardware PWM output for E signal and do all the rest in timer's ISR - data bus direction, R/W, RSx lines and data input/output, as described in my first post. Ok, maybe it's easier to handle E in software to avoid worries about timing details. Minimum E frequency is 100 kHz, so you have plenty of time - 360 cycles for each E edge to be exact - enough for execution of at least 20 lines of simple C code. Just design a nice state machine for that. ;)

When connecting 5 V outputs to non-FT STM32 inputs, you only need to limit the injection current to the values specified in the datasheet. In most cases 1k5..3k resistor will do. In some STM32 there are some TT lines with no injection allowed - then, in addition to a resistor, a 3V3 Zener diode or a violet LED should be connected to the input.

My STM32 stuff on github - compact USB device stack and more: https://github.com/gbm-ii/gbmUSBdevice
mƎALLEm
Technical Moderator
October 27, 2025

Hello @Elektraglide ,

I'm a bit surprised that someone needs to build a tester for a such fossil IC from Motorola. An IC which dates from the 70's in the last century :)!

So I'm bit curious. You said you are planning to make a tester for that parallel interface MC6021 using STM32! Is it just a tester to make sure it's working as expected on all IOs on portA and PortB or you need just to expend the GPIO ports?

 

"To give better visibility on the answered topics, please click on ""Accept as Solution"" on the reply which solved your issue or answered your question."
Elektraglide
Associate II
October 27, 2025

My Williams Defender interface board appears to have died so wanted a quick way of testing other 6821s I have lying around!    Any excuse to mess about with STM32 I will take it!  :)  


Got into trying to use every feature of the STM32F103 building this smooth scrolling, texturemapped demo without any bit banging - just DMA and timers.

https://www.youtube.com/watch?v=I8Wlj3myxoA

mƎALLEm
Technical Moderator
October 27, 2025

I can't see something related to E (Enable pin ) except this timing in the MC6821 datasheet:

mALLEm_1-1761566461904.png

So pulse width low and high are specified as minimum. I think no need to use a timer for that use a simple GPIO + delay and ensure you respect this timing.

To me the first post of @gbm is giving the right steps.

 

"To give better visibility on the answered topics, please click on ""Accept as Solution"" on the reply which solved your issue or answered your question."
Visitor II
February 11, 2026

Did you ever complete this project?  I have a pile of suspect PIAs on my workbench from pinball PCB repairs that need to be validated or tossed... and a some ST dev boards that need a project.