How to force a hard reset (CTRL+BREAK)?

Discuss all aspects of programming here. From 8-bit through to modern architectures.
Post Reply
User avatar
0xC0DE
Posts: 187
Joined: Tue Mar 19, 2019 7:52 pm
Location: The Netherlands
Contact:

How to force a hard reset (CTRL+BREAK)?

Post by 0xC0DE » Tue May 14, 2019 9:43 pm

I know about *FX200,3 and the Break Intercept Vector and the Reset Vector ($FFFC) but I can't figure out how to force a hard reset when the user presses BREAK (not CTRL+BREAK) only. Is it possible?
0xC0DE
Visit my YouTube channel featuring demo videos running on my Acorn Electron emulator Electroniq

User avatar
sweh
Posts: 2028
Joined: Sat Mar 10, 2012 12:05 pm
Location: New York, New York
Contact:

Re: How to force a hard reset (CTRL+BREAK)?

Post by sweh » Tue May 14, 2019 11:12 pm

If you don't care about the machine doing anything while you're waiting for the user to press BREAK then ?&FE4E=127 which disables all the interrupts on the System VIA and so effectively causes the machine to freeze. On BREAK the MOS checks the state of the VIA and if it's not properly configured then assumes the machine is in power-on state and does a full reset.

Now if you do want to keep the computer running, we can use the above _and_the "Break intercept code" (*FX 247/8/9) which is a 3 byte instruction that's called when BREAK is pressed. We can make this JMP to code that does the reset.

So something like

Code: Select all

P%=&900
[
LDA#127
STA&FE4E
JMP(&FFFC)
]
*FX247 76
*FX248 0
*FX249 9
Now when you press the BREAK key this will do a JMP&900 which will turn the VIA interrupts off and then do another reset, which will cause a complete "power on" reset.
Rgds
Stephen

User avatar
0xC0DE
Posts: 187
Joined: Tue Mar 19, 2019 7:52 pm
Location: The Netherlands
Contact:

Re: How to force a hard reset (CTRL+BREAK)?

Post by 0xC0DE » Tue May 14, 2019 11:16 pm

Very useful, thanks!
Does a similar trick exist for the Electron?
I would like my code to work on both machines.
0xC0DE
Visit my YouTube channel featuring demo videos running on my Acorn Electron emulator Electroniq

User avatar
jgharston
Posts: 3497
Joined: Thu Sep 24, 2009 11:22 am
Location: Whitby/Sheffield
Contact:

Re: How to force a hard reset (CTRL+BREAK)?

Post by jgharston » Wed May 15, 2019 2:06 am

0xC0DE wrote:
Tue May 14, 2019 9:43 pm
I know about *FX200,3 and the Break Intercept Vector and the Reset Vector ($FFFC) but I can't figure out how to force a hard reset when the user presses BREAK (not CTRL+BREAK) only. Is it possible?
Not possible.**
You can force a memory-wipe reset (fx200,3), or a power-on reset (fx151,78,127*), but you cannot force a hard reset, as the reset code directly reads the state of the Ctrl key directly. The only reset you can cause that preserves memory is a normal soft reset.

*custom code needed for the Electron, see here.

**well, theoretically possible, by duplicating most of the reset code and running that instead.

Code: Select all

$ bbcbasic
PDP11 BBC BASIC IV Version 0.25
(C) Copyright J.G.Harston 1989,2005-2015
>_

User avatar
jgharston
Posts: 3497
Joined: Thu Sep 24, 2009 11:22 am
Location: Whitby/Sheffield
Contact:

Re: How to force a hard reset (CTRL+BREAK)?

Post by jgharston » Wed May 15, 2019 2:09 am

sweh wrote:
Tue May 14, 2019 11:12 pm
So something like

Code: Select all

P%=&900
[
LDA#127
STA&FE4E
JMP(&FFFC)
]
*FX247 76
*FX248 0
*FX249 9
Now when you press the BREAK key this will do a JMP&900 which will turn the VIA interrupts off and then do another reset, which will cause a complete "power on" reset.
....which will then jump to &900 again, only to find that is has been wiped to zeros, so crash with a BRK:BRK:BRK error. ;)

Code: Select all

$ bbcbasic
PDP11 BBC BASIC IV Version 0.25
(C) Copyright J.G.Harston 1989,2005-2015
>_

User avatar
0xC0DE
Posts: 187
Joined: Tue Mar 19, 2019 7:52 pm
Location: The Netherlands
Contact:

Re: How to force a hard reset (CTRL+BREAK)?

Post by 0xC0DE » Wed May 15, 2019 9:16 am

Thanks to both of you I have pieced it together and it is now working platform independently.
Pressing BREAK now performs a power-on reset on either machine.

A bit more background.
I have written some test code that takes over the entire BBC or Electron, shutting out MOS altogether and claiming the entire memory map. Except for some critical memory locations: &FC, &180-&200, &258, &287 and &D00. My goal is to maximize speed and memory. A much needed power-on reset after pressing either BREAK or CTRL-BREAK will now restore the machine to a usable state :mrgreen:

While experimenting I have found that *FX200,3 (clear memory on BREAK) doesn't play nice with this setup. Maybe the memory clearing also overwrites the Break Intercept Vector (JMP xxxx) at &287? I have now changed it to *FX200,1.
0xC0DE
Visit my YouTube channel featuring demo videos running on my Acorn Electron emulator Electroniq

User avatar
jgharston
Posts: 3497
Joined: Thu Sep 24, 2009 11:22 am
Location: Whitby/Sheffield
Contact:

Re: How to force a hard reset (CTRL+BREAK)?

Post by jgharston » Wed May 15, 2019 2:17 pm

Untested, but this will perform a Hard Reset on MOS 1.20:

Code: Select all

.ForceHardReset
LDA #&8F:LDX #&0C
LDY #&FF:JSR OSBYTE ; Claim NMIs
LDA #&40:STA &D00   ; Disable NMIs
SEI:CLD             ; Disable IRQs, ensure binary mode
LDX #&FF:TXS        ; Reset stack
LDX #&0F:STX &FE42  ; SysVIA PortB=iiiioooo
.LDA08
DEX:STX &FE40       ; Write to I/O latch
CPX #&09:BCS LDA08  ; Loop to set up internal I/O
INX                 ; X=9
.LDA11
TXA:JSR LF02A       ; KeyScan for key X, test links and CTRL
CPX #&80:ROR &FC    ; Rotate keystate in bit 7 into &FC
TAX:DEX:BNE LDA11   ; Loop for key 9 to key 1
STX &028D           ; Clear last BREAK flag
ROL &FC             ; CTRL is now in carry, &FC is keyboard links
SEC                 ; Set CTRL pressed
JMP &DA22           ; Jump to RESET code

Code: Select all

$ bbcbasic
PDP11 BBC BASIC IV Version 0.25
(C) Copyright J.G.Harston 1989,2005-2015
>_

User avatar
jgharston
Posts: 3497
Joined: Thu Sep 24, 2009 11:22 am
Location: Whitby/Sheffield
Contact:

Re: How to force a hard reset (CTRL+BREAK)?

Post by jgharston » Wed May 15, 2019 2:20 pm

0xC0DE wrote:
Wed May 15, 2019 9:16 am
While experimenting I have found that *FX200,3 (clear memory on BREAK) doesn't play nice with this setup. Maybe the memory clearing also overwrites the Break Intercept Vector (JMP xxxx) at &287? I have now changed it to *FX200,1.
A memory-wipe reset clears everything from &400 upwards, so leaves the Break Intercept Vector at &287/8/9 in place, but wipes out anything it jumps to if that is in memory above &400.

Another gotcha is that on the Compact a memory-wipe reset wipes the sideways RAM as well!

Code: Select all

$ bbcbasic
PDP11 BBC BASIC IV Version 0.25
(C) Copyright J.G.Harston 1989,2005-2015
>_

User avatar
0xC0DE
Posts: 187
Joined: Tue Mar 19, 2019 7:52 pm
Location: The Netherlands
Contact:

Re: How to force a hard reset (CTRL+BREAK)?

Post by 0xC0DE » Wed May 15, 2019 2:30 pm

Makes me wonder: what does a hard reset do that a power-on reset won't do, or vice versa?
0xC0DE
Visit my YouTube channel featuring demo videos running on my Acorn Electron emulator Electroniq

User avatar
hoglet
Posts: 8097
Joined: Sat Oct 13, 2012 6:21 pm
Location: Bristol
Contact:

Re: How to force a hard reset (CTRL+BREAK)?

Post by hoglet » Wed May 15, 2019 2:43 pm

0xC0DE wrote:
Wed May 15, 2019 2:30 pm
Makes me wonder: what does a hard reset do that a power-on reset won't do, or vice versa?
A hard reset (Ctrl-Break) doesn't wipe memory.

The reset code starts at D9CD; take a look a Jonathan's excellent commented disassebly:
http://mdfs.net/Docs/Comp/BBC/OS1-20/D940
Last edited by hoglet on Wed May 15, 2019 2:45 pm, edited 1 time in total.

User avatar
sweh
Posts: 2028
Joined: Sat Mar 10, 2012 12:05 pm
Location: New York, New York
Contact:

Re: How to force a hard reset (CTRL+BREAK)?

Post by sweh » Wed May 15, 2019 2:58 pm

jgharston wrote:
Wed May 15, 2019 2:09 am
sweh wrote:
Tue May 14, 2019 11:12 pm
So something like

Code: Select all

P%=&900
[
LDA#127
STA&FE4E
JMP(&FFFC)
]
*FX247 76
*FX248 0
*FX249 9
Now when you press the BREAK key this will do a JMP&900 which will turn the VIA interrupts off and then do another reset, which will cause a complete "power on" reset.
....which will then jump to &900 again, only to find that is has been wiped to zeros, so crash with a BRK:BRK:BRK error. ;)
Not in my tests it doesn't. I did test this before posting :-) The power on reset clears this memory as well.

If it was a problem then the code at &0900 could simply *fx247,0,0 to stop the loop as a first action.
Rgds
Stephen

User avatar
1024MAK
Posts: 8668
Joined: Mon Apr 18, 2011 4:46 pm
Location: Looking forward to summer in Somerset, UK...
Contact:

Re: How to force a hard reset (CTRL+BREAK)?

Post by 1024MAK » Wed May 15, 2019 3:03 pm

Keep in mind that a ‘power on reset’, a ‘control-break’ reset and a ‘break’ (on it’s own) reset are all different to each other.

A power on reset assumes that the machine has just been switched on, and therefore it clears most of the RAM to a known state.

The Break key (normal reset line) resets the CPU and most of the other chips (that have a reset input) but not the system VIA (6522). This has a separate reset circuit that is only activated during a power up from off.

Mark

User avatar
Rich Talbot-Watkins
Posts: 1444
Joined: Thu Jan 13, 2005 5:20 pm
Location: Palma, Mallorca
Contact:

Re: How to force a hard reset (CTRL+BREAK)?

Post by Rich Talbot-Watkins » Wed May 15, 2019 3:05 pm

I believe a memory clear reset (*fx 200,2) is somewhat distinct from a power-on reset too, though I couldn't tell you exactly how.

User avatar
1024MAK
Posts: 8668
Joined: Mon Apr 18, 2011 4:46 pm
Location: Looking forward to summer in Somerset, UK...
Contact:

Re: How to force a hard reset (CTRL+BREAK)?

Post by 1024MAK » Wed May 15, 2019 3:14 pm

One other clarification. Technically a running program can’t actually perform any hardware reset, as it does not actually affect the reset line. So any software that is going to perform a ‘system restart’ must take care to set up all the chips that have registers, and put appropriate values in these registers. As the chips otherwise will be left with the last values that were written to their registers.

Mark

User avatar
Rich Talbot-Watkins
Posts: 1444
Joined: Thu Jan 13, 2005 5:20 pm
Location: Palma, Mallorca
Contact:

Re: How to force a hard reset (CTRL+BREAK)?

Post by Rich Talbot-Watkins » Wed May 15, 2019 3:46 pm

Yeah - the best way to force a reset is:

Code: Select all

PRINT "Please press BREAK"
REPEAT UNTIL FALSE
:wink:

User avatar
0xC0DE
Posts: 187
Joined: Tue Mar 19, 2019 7:52 pm
Location: The Netherlands
Contact:

Re: How to force a hard reset (CTRL+BREAK)?

Post by 0xC0DE » Wed May 15, 2019 10:27 pm

I definitely learned a lot from this thread, so thanks to all.
All things considered, I now have code that runs on the BBC and the Electron. And it combines *FX200,3 with a fake power-on reset. It works really well! The solution was indeed to locate the break intercept handlers below &400.
0xC0DE
Visit my YouTube channel featuring demo videos running on my Acorn Electron emulator Electroniq

User avatar
tricky
Posts: 3419
Joined: Tue Jun 21, 2011 8:25 am
Contact:

Re: How to force a hard reset (CTRL+BREAK)?

Post by tricky » Thu May 16, 2019 12:47 pm

0xC0DE wrote:
Wed May 15, 2019 9:16 am
... Except for some critical memory locations: &FC, &180-&200, &258, &287 and &D00. ...
I'm not that generous, I use ESCAPE to quite as above and grab NMI before I start (may not be necessary as I'm not sure it guarantees stopping NMIs) and then use everything except &FC, &204/5 and a few bytes of stack ;)
I'm still a little miffed that the IRQ vector doesn't point into RAM (maybe &200) so that the OS could be further removed as well as its IRQ overhead. That reminds me, I really should fit the BooBip OS RAM module so that I can do this - would it be cheating to use that for demo competitions, or do they need "stock" OS ROMs?

User avatar
Rich Talbot-Watkins
Posts: 1444
Joined: Thu Jan 13, 2005 5:20 pm
Location: Palma, Mallorca
Contact:

Re: How to force a hard reset (CTRL+BREAK)?

Post by Rich Talbot-Watkins » Thu May 16, 2019 1:47 pm

In the past I've tried to contrive that bit 1 of &258 is set, so BREAK does a memory clear reset, and that &287 contains anything but &4C so it doesn't appear as a BREAK intercept. I don't think there's anything you can do about NMIs though - if there's any device hooked up which can generate them, the CPU will jump to &D00 without warning - so I always put an RTI there regardless.

User avatar
0xC0DE
Posts: 187
Joined: Tue Mar 19, 2019 7:52 pm
Location: The Netherlands
Contact:

Re: How to force a hard reset (CTRL+BREAK)?

Post by 0xC0DE » Thu May 16, 2019 2:25 pm

"Generous" :shock: :mrgreen:

I forgot to mention &0204 IRQ1V of course.
These are the only instructions I have to allow the MOS to execute (on the Electron):

Code: Select all

&DAE7:

STA &FC
PLA
PHA
AND #&10
BNE &DB06
JMP (&0204)
My interrupt handler doesn't return to MOS at all. Just handles and acknowledges/clears IRQs by itself, before LDA &FC: RTI.

Initialization code contains: *TAPE, claim NMI, host check, disabling PLUS1 (on Electron), relocation, resetting the stack, and more.
0xC0DE
Visit my YouTube channel featuring demo videos running on my Acorn Electron emulator Electroniq

Post Reply