Menu

#167 Printing text to console in ucsim_6502

None
closed
nobody
None
5
2024-05-26
2022-07-22
Under4Mhz
No

I'd like to print some output on the 6502 simulator for testing. I've tried the following:

// GPL 2.0
#include <stdio.h>
#include <stdint.h>

void main() {

    printf( "Start\n" );

    printf( "End\n" );
}

#ifdef __SDCC
unsigned char __at 0x9ff0 sif; // Not sure?
int putchar(int c)
{
    sif = 'p';
    sif = c;

    return c;
}
#endif
sdcc -mmos6502 ./print_test65.c  -o print_test65.ihx && ucsim_mos6502 -I if=memory[0xe500] print_test65.ihx
uCsim 0.7.5, Copyright (C) 1997 Daniel Drotos.

0> Simif: memory is not a valid address space name

What's a valid address space name for 6502? I tried xram as well. The help says "memory", but presumably it's processor specific. Is there a list of valid address space names per processor?

Is my sif address correct/reasonable?

Discussion

  • Philipp Klaus Krause

    I do not use the 6502 port, but ports/uc6502/support.c has:

    static unsigned char * volatile sif;
    
    void
    _putchar(unsigned char c)
    {
      *sif= 'p';
      *sif= c;
      return;
    }
    
    void
    _initEmu(void)
    {
      sif= (unsigned char *)0xdfff;
    }
    
    void
    _exitEmu(void)
    {
      *sif= 's';
      return;
    }
    
     
  • Daniel Drotos

    Daniel Drotos - 2022-07-22

    On Fri, 22 Jul 2022, Under4Mhz wrote:

    #ifdef __SDCC
    unsigned char __at 0x9ff0 sif; // Not sure?

    6502 is pure Neumann, there is no IO space like in z80, so I think sfr
    style is not usable. You shoud use a pointer, see the answer by
    Philipp (and don't forget the "volatile").

    What's a valid address space name for 6502? I tried xram as well.
    The help says "memory", but presumably it's processor specific. Is
    there a list of valid address space names per processor?

    Use "info mem" or "mem info" to check names of address spaces. In most
    Neumann cases uCsim use "rom" as the name:

    0> info mem
    Memory chips:
       0x000000-0x0000ff      256 variable_storage (32,%08x,0x%02lx)
       0x000000-0x00ffff    65536 rom_chip (8,%02x,0x%04lx)
    Address spaces:
       0x000000-0x0000ff      256 variables (32,%08x,0x%02lx)
       0x000000-0x00ffff    65536 rom (8,%02x,0x%04lx)
    Address decoders:
       variables 0x00 0xff -> variable_storage 0x00 activated
       rom 0x0000 0xffff -> rom_chip 0x0000 activated
    0> 
    

    Is my sif address correct/reasonable?

    simif configuration must specify the address where the program writes
    the commands, so in case of:

       sif= (unsigned char *)0xdfff;
    

    you have to config simif as:

       ucsim_mos6502 -I if=rom[0xdfff] ...
    

    Any location can be used if it is not used by the program in other
    ways.

    Daniel

     

    Last edit: Maarten Brock 2022-07-22
  • Maarten Brock

    Maarten Brock - 2022-07-22

    I am totally surprised.
    static unsigned char * volatile sif;
    Why would you want to make the pointer volatile and not the pointed to value?

    unsigned char x, y;
    sif = &x; // volatile after * prevents this line to be optimized out
    sif = &y;
    *sif = x; // lack of volatile before * allows this line to be optimized out!
    *sif = y;
    

    Or have I totally lost touch of the rules?

    Maarten

     
    • Philipp Klaus Krause

      I don't know why that pointer is volatile (and I'm not very familiar with the test infrastructure and its interaction with uCSim - Daniel and Felix are the experts there).

       

      Last edit: Philipp Klaus Krause 2022-07-22
    • Daniel Drotos

      Daniel Drotos - 2022-07-22

      On Fri, 22 Jul 2022, Maarten Brock wrote:

      static unsigned char * volatile sif;
      Why would you want to make the pointer volatile and not the pointed to value?

      I think you are right, it is wrong and this mistake was copied to
      several support.c files. Surprisingly it worked this way for years.
      I've checked generated code several times in the past and
      *sif=c; was never eliminated.

      Daniel

       
      • Philipp Klaus Krause

        Yes, SDCC is lacking a good pointer analysis, and thus often won't optimize out pointer accesses where it should:
        [feature-requests:#365]

         

        Related

        Feature Requests: #365

  • Under4Mhz

    Under4Mhz - 2022-07-23

    Great, thanks for your help. The following worked well:

    // GPL 2.0
    #include <stdio.h>
    #include <stdint.h>
    
    void main() {
    
        printf( "Start\n" );
        printf( "End\n" );
    }
    
    #ifdef __SDCC
    unsigned char __at 0xdfff sif;
    int putchar(int c)
    {
        sif = 'p';
        sif = c;
    
        return c;
    }
    #endif
    
    $ sdcc -mmos6502 ./print_test65.c  -o print_test65.ihx && ucsim_mos6502 -I if=rom[0xdfff] print_test65.ihx
    uCsim 0.7.5, Copyright (C) 1997 Daniel Drotos.
    0> Loading from print_test65.ihx
    2742 words read from print_test65.ihx
    r
    Simulation started, PC=0x000200
    Start
    End
    
     
    • Maarten Brock

      Maarten Brock - 2022-07-24

      I definitely recommend to make your sif volatile.
      I don't know what writing 'p' is supposed to do, but I doubt you would want that line removed. Yet, without volatile the compiler can deduce that the 'p' value is never used and thus its assignment can be removed.

       
  • Daniel Drotos

    Daniel Drotos - 2024-05-26
    • status: open --> closed
    • Group: -->
     

Log in to post a comment.

Auth0 Logo