example: postcode.c - retrotruestory/M1DEV GitHub Wiki

/*
 * Demo of writing to post code display.  Must be root.
 */
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <magic1.h>

static int dmemfd = 0;
void to_post( unsigned char val )
{
  if (!dmemfd) {
    if ((dmemfd = open("/dev/dmem", O_WRONLY)) == -1) {
      fprintf(stderr,"Couldn't open /dev/dmem.  Are you root?");
      exit(-1);
    }
  }

  lseek(dmemfd, POST_CODE_BASE, SEEK_SET);
  write(dmemfd, &val, 1);
}

int main() {
  unsigned char val;
  while (1) {
    to_post(val++);
    sleep(1);
  }
  return 0;
}

As far as the postcode example, what you posted should work after being compiled with clcc on your Linux system (after fixing the #include <> statements, which seem to have been dropped on the web page). However, a version of this code can also be compiled directly on Magic-1 using the subc subset C compiler. Subc doesn't support long (32-bit) ints, which the lseek() call requires. However, you can lie to subc and tell it that lseek() takes an extra int parameter and pass the long int in two parts.

The following code can be entered on your Magic-1 system and compiled there. However, I notice that I neglected to properly set permissions on at least one of the include files in /usr/include. So, before attempting to compile this code "su" to root (password magic), and then "chmod 444 /usr/include/*.h". Then, "cc -o postcode postcode.c" and give it a go.

Here's the code:

Here's the code:

/*
 * Demo of writing to post code display.  Must be root.
 */
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
//#include <magic1.h>

#define POST_CODE_BASE 0xFFC0

static int dmemfd = 0;
void to_post(char val)
{
  if (!dmemfd) {
    if ((dmemfd = open("/dev/dmem", O_WRONLY)) == -1) {
      fprintf(stderr,"Couldn't open /dev/dmem.  Are you root?");
      exit(-1);
    }
  }

/*
  Because lseek expects a long for the base, we send the
  high 16 bits as a separate argument (the "0" here).
*/
 
  lseek(dmemfd, 0, POST_CODE_BASE, SEEK_SET);
  write(dmemfd, &val, 1);
}

int main() {
  char val;
  val = 0;
  while (1) {
    to_post(val++);
    sleep(1);
  }
  return 0;
}


...Bill

⚠️ **GitHub.com Fallback** ⚠️