Search code examples
cemulationbasic6502

EhBASIC input on 6502 emulator


I have been working on this 6502 emulator for a while, and I'm tying to get a simple Enhanced BASIC ROM to work. Although it lacks precise clock timing, the emulator did pass AllSuiteA.asm. With EhBASIC, I've managed to get some output by printing the value of $F001 when a read is done to that address.

if(lastwrite == 0xF001)
    {
    printf("%c",CPUMEM[0xF001]);
    }

However, I have no idea how to emulate the input process. This post states that whenever EhBASIC wants input, It will poll $F004. But my current code seems to have two problems:

while(1)
    {
    decodeandexecute();
    if(lastread == 0xF004)
        {
        inputchar = getchar();
        CPUMEM[0xF004] = inputchar;
        }
    if(lastwrite == 0xF001)
        {
        printf("%c",CPUMEM[0xF001]);
        }
    }
  • Input is only possible through individual letters (Expected)
  • After the program asks for memory size, giving any input will just cause a loop of reading from $F004 (LDA $F004) - i.e. I can't let EhBASIC know when to stop receiving inputs

I want to know an effective method of entering a string of characters, and getting passed "memory size?".

Additionally, if I want to let EhBASIC calculate the memory size automatically, what should I input to $F004?

I'm pretty much a newbie in this area....


Solution

  • I see you use getchar in the code and if I remember correctly that is a blocking call (it will wait until someone presses some key).

    In the manual of ehbasic it says:

    How to.
    The interpreter calls the system routines via RAM based vectors and, 
    as long as the requirements for each routine are met, these can be changed 
    on the fly if needs be.
    
    All the routines exit via an RTS.
    
    The routines are ... 
    
    Input 
    This is a non halting scan of the input device. If a character is ready it 
    should be placed in A and the carry flag set, if there is no character then A,
    and the carry flag, should be cleared.
    

    One way to deal with this is to use two threads. One thread that runs the emulation of the 6502 running ehbasic and another thread that polls the keyboard. Then let the polling thread push any input key strokes into a small buffer from which the ehbasic input routine can use.

    Manual: http://www.sunrise-ev.com/photos/6502/EhBASIC-manual.pdf

    UPDATE Reading the question/answer you linked to, I see it is a modified ehbasic. Your keyboard polling thread should place the keystrokes read in $F004 (and after a while clear F004 again - if I understand the instructions).

    UPDATE 2 As a debugging tip: In you first version simply have a string with a fixed input such as 10 print "hello" 20 goto 10 and feed $f004 from there. That way you don't have to worry about any problems with using an actual keyboard.