[cc65] Re: signals

From: Jonathan Graham Harston <jgh1arcade.demon.co.uk>
Date: 2005-05-30 16:04:59
> Message-ID: <4294A998.2010908@brahms.demon.co.uk>
 
dominic beesley <dominic@brahms.demon.co.uk> wrote:
> Some quick questions on signals, trying to get escape and
> error handling working for BBC...
[snip]
> I want to override _sig_dfl for BBC micro so that it traps
> SIGINT and aborts to the OS when Escape is pressed and not
> captured. What is the best way of doing this? I don't want to move
 
The best way is to not do this. The default action of Escape is to
set a flag that the foreground program checks. The background (ie,
the escape) should not bomb out on the foreground's behalf. The
foreground may be halfway through a disk write!
 
The foreground has to explicity check the escape flag if it wants
to act upon it. BASIC does this between every statement. A BASIC
statement itself is uninterruptable. Try:
 
A$=STRING$(255,"*")
PRINT A$;A$;A$;A$;A$;A$;A$;A$;A$;A$;A$;A$;A$;A$;A$;A$;A$;A$
 
and you will see that you cannot Escape from the PRINT statement,
only prior to it and after it.
 
A 'C' program compiles to a chunk of machine code, essentially
what a single BASIC statement is. Effectively, BASIC's ':' and
'eol' are effectively a <if escape, generate error> command. If
you do not explicity do that yourself in any program you write,
Escape is going to be ignored.
 
Consider the machine code pseudo-code: [1]
 
.label
print "hello"
goto labal
 
This will go on forever until a reset. Compare this with: [2]
 
.label
print "hello"
check escape
if notset goto lebal
ret
 
This will loop as long as the escape state is not set. When escape
is set, the loop terminates.
 
BASIC checks the Escape flag and generates an "Escape" error. VIEW
checks the Escape flag and toggles between Command and Editing.
Repton checks the Escape flags and kills the current life.
 
If you want to change away from the default action of escape, you
should do some other /background/ action, such as setting a 'C'
program variable. Something like:
 
...
signal(_sig_esc,my_esc());
...
 
my_esc()
{ a_global_variable=1; }
 
Remember, in 'C', you're effectively writing machine code, so
*you* the programmer have to do the background checking that a
higher-level language does for you. I would recommend you provide
some library functions to check for and acknowledge Escape, so
that you can do something like:
 
for(;;) {
  printf("Hello\n");
  if(os_checkescape()) {
    os_ackescape();
    break;
    }
  }
 
signalling a BRK (ie, a BBC error) is allowed to change the
program flow, as a BRK on the BBC is a foreground action, the
equivalent of BASIC's ON ERROR. With the appropriate code, you
should be able to do something like this:
 
main()
{
signal(_sig_brk, myerror());
for(;;) {
  printf("Hello\n");
  if(os_checkescape()) { os_error(17, "Escape"); }
  }
}
 
myerror()
{
os_report();
main();
}
 
Remember, that the code that processes BRK must reset the
language's stacks, otherwise each BRK pushes the program further
and further into the stack.
 
JGH
 
[1]
.label
ldx #0
.loop
lda hello,x:jsr osasci:inx:cmp #13:bne loop
jmp label
.hello
equs "Hello":equb 13
 
[2]
.label
ldx #0
.loop
lda hello,x:jsr osasci:inx:cmp #13:bne loop
bit &FF:bpl label  ; loop back if Escape not set
rts
.hello
equs "Hello":equb 13
 
-- 
J.G.Harston - jgh@arcade.demon.co.uk - mdfs.net/User/JGH
BBC BASIC for Windows and Internationalisation
  See http://mdfs.net/Software/BBCBasic/Windows/ProgTips
----------------------------------------------------------------------
To unsubscribe from the list send mail to majordomo@musoftware.de with
the string "unsubscribe cc65" in the body(!) of the mail.
Received on Mon May 30 22:17:49 2005

This archive was generated by hypermail 2.1.8 : 2005-05-30 22:17:52 CEST