User Tools

Site Tools


mt6502:helloworld
Translations of this page:


Multithreading on 6502/6510 retrocomputers

"HELLO WORLD" WITH TWO PROTOTHREADS

The code on tutorial_protothread1.c shows how to make two threads that, in competition, will access the CPU to print “Hello world”. To tell them apart, we'll add the protothread number at the bottom.

Here we comment on the code:

  mr_protothread firstThread;
  mr_protothread secondThread;

The mr_protothread type is specific to the MIDRES library, and allows you to reserve the two bytes needed to store the status (or “local continuation”) of the protothread. In this case, we define two variables because we are going to start two simultaneous protothreads.

  MR_PT_THREAD(first) {

This is the standard definition to indicate that a function is actually a protothread. first is the name of the function, which must then be called by giving the address of the variable firstThread.

      MR_PTI_BEGIN();

The protothread actually starts here.

      while (1) {
          printf("Hello world 1\n");

We introduce here an infinite loop, which makes the logic of this function execute, which will only print the words “Hello world 1”.

          MR_PTI_YIELD();

Here the program indicates that it can release control to other threads. When others do the same, control will revert to the statement following this one.

      }
      MR_PTI_END();
  }

Here ends the loop of the first thread and, moreover, it is reported that from this line that also ends the definition of the protothread.

  MR_PT_THREAD(second) {
      MR_PTI_BEGIN();
      while (1) {
          printf("Hello world 2\n");
          MR_PTI_YIELD();
      }
      MR_PTI_END();
  }
  

The second function is identical to the first, except that it will print “Hello world 2”.

  void tutorial_protothread1() {
      while (1) {
          first(&firstThread);
          second(&secondThread);
      }
  }

This is the main loop of the program, which continuously calls the first function and the second function.

HOW TO COMPILE

You can compile the tutorial with this command line:

  make tutorial=PROTOTHREAD1 target=... all

Target allowed are:

  • Commodore VIC-20 expanded (vic2024);
  • Commodore 64 (c64);
  • Commodore 128 (c128);
  • Commodore PLUS/4 (plus4);
  • Atari (8 bit) (atari).

THE RESULT