Splitting a 16-bit value to two 8-bit values in C

1/27/2023: Hello, everyone. This page continues to be one of the most-viewed page on my site. Can you leave a comment and tell me what led you here? Thanks! -Allen

Recently in my day job, I came across some C code that just felt inefficient. It was code that appeared to take a 16-bit integer and split the high and low bytes in to two 8-bit integers. In all my years of C coding, I had never seen it done this way, so obviously it must be wrong.

NOTE: In this example, I am using modern C99 definitions for 8-bit and 16-bit unsigned values. “int” may be different on different systems (it only has to be “at least” 16-bits per the C standard. On the Arduino it is 16-bits, and on my PC it is 32-bits).

uint8_t  bytes[2];
uint16_t value;

value = 0x1234;

bytes[0] = *((uint8_t*)&(value)+1); //high byte (0x12)
bytes[1] = *((uint8_t*)&(value)+0); //low byte  (0x34)

This code just felt bad to me because I had previously seen how much larger a program becomes when you are accessing structure elements like “foo.a” repeatedly in code. Each access was a bit larger, so it you used it more than a few times in a block of code you were better off to put it in a temporary variable like “temp = foo.a” and use “temp” over and over. Surely all this “address of” and math (+1) would be generating something like that, right?

Traditionally, the way I always see this done is using bit shifting and logical AND:

uint8_t  bytes[2];
uint16_t value;

value = 0x1234;

bytes[0] = value >> 8;     // high byte (0x12)
bytes[1] = value & 0x00FF; // low byte (0x34)

Above, bytes[0] starts out with the 16-bit value and shifts it right 8 bits. That turns 0x1234 in to 0x0012 (the 0x34 falls off the end) so you can set bytes[0] to 0x12.

bytes[1] uses logical AND to get only the right 8-bits, turning 0x1234 in to 0x0034.

I did a quick test on an Arduino, and was surprised to see that the first example compiled in to 512 bytes, and the second (using bit shift) was 516. I had expected a simple AND and bitshift to be smaller, but apparently, on this processor/compiler, getting a byte from an address was smaller. (I did not tests to see which one used more clock cycles, and did no experiments with compiler optimizations.)

On a Windows PC under GNU-C, the first compiled to 784 bytes, and the second to 800. Interesting.

I ran across this code in a project targeting the Texas Instruments MSP430 processor. The MSP430 Launchpad is very Arduino-like, and previous developers had to do many tricks to get the most out of the limited RAM, flash and CPU cycles of these small devices.

I do not know if I can get in the habit of doing my integer splits this way, but perhaps I should retrain myself since this does appear incrementally better.

Update: Timing tests (using millis() on Arduino, and clock() on PC) show that it is also faster.

Here is my full Arduino test program. Note the use of “volatile” variable types. This prevents the compiler from optimizing them out (since they are never used unless you uncomment the prints to display them).

#define OURWAY

void setup() {
   volatile char bytes[2];
   volatile uint16_t  value;

   //Serial.begin(9600);

   value = 0x1234;

#ifdef OURWAY
   // 512 bytes:
   bytes[0] =  *((uint8_t*)&(value)+1); //high byte (0x12)
   bytes[1] =  *((uint8_t*)&(value)+0); //low byte  (0x34)
#else
   // 516 bytes:
   bytes[0] = value >> 8;     // high byte (0x12)
   bytes[1] = value & 0x00FF; // low byte  (0x34)
#endif

   //Serial.println(bytes[0], HEX); // 0x12
   //Serial.println(bytes[1], HEX); // 0x34
}

void loop() {
   // put your main code here, to run repeatedly:
}

Program like it’s 1980!

Just for fun, let’s pretend it is the summer of 1980, and you just walked in to a Radio Shack and saw the brand new TRS-80 Color Computer. Unlike the original TRS-80 Model I, this thing could hook up to a color television (instead of a monitor) and display colors and make sound! Amazing.

If you picked one up (and a cable to hook up a cassette recorder for loading and saving programs), what would you do with it? I propose a fun challenge to find out.

Rules:

Your program can use any modern knowledge, but must run on a stock 1980 4K CoCo running Color BASIC 1.0.

That’s it. However, certain things would not be possible to create ON that machine. For instance, the EDTASM assembler ROM Pak required at least 16K, so any assembly language written on a 4K CoCo had to be hand assembled (somehow). If someone actually does that, it should be noted and given special consideration.

I propose the entries will be created in one of the following ways:

Native versus Expanded versus Cross Hosted – a program could be written on an actual 4K CoCo (native), or on a more expanded CoCo (16K CoCo 1, 512K CoCo 3, etc.), or compiled using PC/Mac/Linux cross compiler tools.

Real versus Emulated – likewise, the coding could be done on a real CoCo, or a virtual one in an emulator.

Ultimately, doing it actually on a native 4K real CoCo would be the only way it could have been done in 1980, but if someone wants to participate using an emulated one that is fine (but it will be noted, just so we can congratulate someone for actually still having a 4K CoCo around that never got upgraded).

More impressive things could probably be done using a later environment (EDTASM on a larger CoCo rather that native hand compiling), or using PC tools. That’s a different type of development, but ultimately, all should run on a stock 4K CoCo.

If you might want to follow this as we figure out how to approach it (or participate), details will be at the CoCoPedia wiki. In coming weeks I will make updates as we figure out more to this challenge:

http://www.cocopedia.com/wiki/index.php/CoCoCoding_1980_Contest

iOS MFI game controller bluetooth protocol

Starting with iOS 7, Apple added official support for game controllers on an iPhone, iPad or iPod touch. Previously, the iCade standard was used, acting as a keyboard device sending key presses for button up and button down.

I have yet to find any place the discusses how these new controllers work. Has anyone reverse engineered it? Does it have digital rights management so you can’t pair using regular devices? Does anyone know?

1983: From the archives…

My first computer was a Commodore VIC-20 in 1982. In 1983, I switched to a Radio Shack TRS-80 Color Computer. Sometime in 1983, I wrote a bulletin board system (BBS) that could run from cassette tape. I previously discussed this old *ALL RAM* BBS when I converted it to run on an Arduino.

People who know me from the “CoCo Community” (CoCo = Color Computer) have heard this story before, and know that my BBS ran in Houston, Texas after I converted it to use disk drives. It was called Cyclop’s Castle, and the system operator (SysOp) was a guy named Graham who was, I believe, the cousin of another CoCo user I knew there, Trevor.

A few years ago, thanks to social networking, I was put back in touch with both of them, and went through the common process or reliving old memories and discussing things such as this old BBS. Until this week, though, Cyclop’s Castle was only a memory. While I may have a copy somewhere on an ancient floppy diskette, I had no idea where it would be in 1988 when I rediscovered my old *ALL RAM* BBS code and released it as shareware.

Earlier this week, while going through my storage room, I came across tons of old paperwork, including a sheet of paper with pencil notes for Cyclop’s Castle. It had the login messages, user level names and other things that were customized. I also found three printouts of some version of Cyclop’s Castle. The ink is faded, but I do hope it is readable enough for me to type it back in and recover this “lost” program.

It turns out, I did several upgrades to the system. In the original *ALL RAM*, only a user name, password, and access level was stores. For Cyclop’s Castle, it also took the caller’s phone number. There were a number of other small enhancements as well, which I should have rolled back in to the official *ALL RAM* when I posted it in 1988, but I had completely forgotten.

There may be more to tell about the tale of *ALL RAM* BBS. I’ll post more when I get time to go through more storage. Who knows what else I will find?

80’s text-based “where in the solar system” game?

I was calling bulletin board systems (BBS) in Houston, Texas between 1982 to 1984. After that, I moved to a small town with no such systems, and other than the occasional long distance call, I wouldn’t have anything to dial again until 1987 when I moved to a less-small town.

I recall there being an online game called “Where in the solar system”. It would present a description of a planet, and you would have to guess which planet it was. I expect it was some existing program found in “GAMES IN BASIC”-type books or something. Someone had taken that game (or wrote a clone) and changed it to be “Where in Six Flags” (referring to the original Six Flags over Texas near Dallas). It would describe rides instead of planets. I recall it describing things like “you can hear screams” or “you feel vibrations.”

Does this ring any bells to anyone out there? There certainly were localized original programs running in Houston (several BBSes I dialed ran on custom software which never existed anywhere else, and I contributed a number of small programs to Apple NETWORKS BBSes which I don’t expect ever got spread around). I wonder if any of this got preserved.

I bring this up after I uncovered some old paperwork last night. I found my original hand-written instructions for the *ALL RAM* BBS I had written. I did not have a printer at the time. I also found notes for the upgraded/customized version that a friend ran in Houston (Cyclop’s Castle) and discovered it had more things added to it than I recalled. I hope to go through all my old files and scan in all my documents to preserve them, and also clone all the floppy disks I have to disk images.

It does make me wonder how much has just been lost and discarded over the years.

Night Trap (classic FMV) on Kickstarter.

https://www.kickstarter.com/projects/1018579240/night-trap-revamped

The classic full motion video game, Night Trap, is coming back, if they get Kick Starter funding.

I played Night Trap on the Sega CD, though it was later ported to other systems. It was one of the first, if not the first, live action full motion video games. It starred Dana Plato (from Diff’rent Strokes) as the heroin in a campy B-movie style horror flick.

It is noteworthy because of the controversy that sprung up around it, led by Nintendo, which caused the industry to adopt video game ratings. That alone makes this quite the historic artifact.

The original developers are wanting to re-release it for modern systems, and have gone back to the original 35mm movie film and digitized it in full resolution letting us see, for the very first time, what the movie was meant to look like (and not the low resolution 64-color versions we saw on the Sega).

Very cool. Check it out.

Using Termcap – part 3

See also: Part 1 and Part 2.

The termcap file is a text file found in /etc/termcap on a Unix system. As it was ported to other operating systems, the default location would change accordingly. The contents of the termcap file is basically a series of entries for each terminal supported. Each entry contains various elements separated by colons. To make the file more readable, a backslash can be used at the end of a line to break it up.

See the Wikipedia page for a summary, or the GNU Software page for a more descriptive summary… Or see this for a vague summary:

For example:

2characterterminalname|longerterminalname|verboseterminalname:
  :capability=characetersqeuence:
  :capability=charactersequence:

The 2 character name is legacy and is no longer used, but remains for ancient backwards compatibility. For a DEC VT-52 terminal, it might look like this:

dw|vt52|DEC vt52:
  :cr=^M:do=^J:nl=^Jbl=^G:
  ...etc...

Each capability has a two character abbreviation. Above, we see that to generate a carriage return (cr) we send a control-M (enter key). A new line (nl) is ^J. The bell (bl) is a ^G (beep!). There are many other simple codes.

For moving the cursor position, a DEC VT52 terminal used the sequence: ESCAPE followed by [ (left bracket) followed by line followed by semicolon followed by column followed by H.

ESCAPE [ 10 ; 4 H

That would mean move the cursor to line 10, column 4. To represent sequences like this with variables inside of them (line, column, etc.), there are more complex termcap entries:

:cm=E%i%d;%dH:

Above, E represents ESCAPE (just like ^ represents CONTROL). %i is a special flag that means “increment the two values supplied” (base 1 numbering) then the two %ds are the variables similar to a C printf() call.

The %i is because termcap assumes base 0, so an 80 column screen would be 0-79. The VT terminal (and PC ANSI, I think) assume base 1, 1-80, so to make it universal, all termcap applications expect a screen that is base 0 (0-79) and the entry knows whether or not to output 0-79 or 1-80. Fun.

Termcap has pages of codes for all kinds of features, like cursor up, delete line, clear screen, clear to end of line, etc. If a terminal does not support a feature, the entry is not present. Applications that use termcap will query these capabilities then use what they can. In my situation, I needed “cm” for cursor movement — and if that feature was not there, I couldn’t work (or, better, I could default to a mode of just lines of text).

There are more advanced features where a termcap entry can reference another entry. For instance, there were series of terminals made and as new models came out, they added new features but maintained the earlier ones as well. The first version terminal would have an entry, then the “v2” terminal would have an entry that described the new features, but by adding a capability of “tc=terminal-v1” or whatever, it would get any other capabilities from the “terminal-v1” entry.

This cuts down on redundant information but also means you can’t just look at one termcap entry and necessarily know everything the terminal does. If you were writing your own code to parse a termcap file, you would have to take this in to consideration.

In a C program that will be linking to the termcap library, to load the terminal type you want, you need a buffer for it to be loaded (2K is the defined max size):


char term_buffer[2048];

…and then you just use the termcap tgetent() function:


status = tgetent(term_buffer, "ANSI");

If the termcap file is found, and there is an entry called “ANSI”, it will be copied in to the term_buffer. By checking for errors (always a good idea), you will know if the entry was not found.

But hard coding is bad. What if this code ever runs on a non-ANSI terminal? Termcap programs typically read the TERM environment variable, then get whatever that is set to. In windows you might “set TERM=ansi” and on Linux you might “export TERM=vt100”. Then the C program would query that environment variable first:


char termtype;
termtype = getenv("TERM");
if (termtype==NULL) { / handle error if env var not set */ }

termtype will come back pointing to whatever the TERM environment variable is set to (“ANSI” in the windows example above, or “vt100” in the Linux example above). Then the tgetent() is done using that response:


status = tgetend(term_buffer, termtype);

If both of those are successful, the individual capabilities can be loaded using the tgetstr() function. tgetstr() will parse capabilities in the loaded termcap entry and write them to a buffer that is processed to be the actual output (less any variables that get substituted when the actual sequence is used later). For instance, the termcap entry might say:

:bl=^G:

…but when you use tgetstr() to parse for the “bl” entry, it will write out the control-G (ASCII 7) character in the output buffer. Basically, it converts all the E (escape) and ^X (control) ASCII characters to what they really represent. This saves work later when they are output to the screen.

A second buffer (that must remain around) needs to be allocated to store the resulting output. Most examples also do a 2K buffer:


char capbuff[2048]; // output sequences are stored here

Then, as each capability is obtained, a pointer is passed in to where the output should be written, and when the call returns, that pointer is advanced to the next place in the buffer where the next capability will go. As tgetstr() is called over and over, the pointer increments filling up the output buffer with entries, and returning the location where each one the user cares about is located within that buffer.


char *tempPtr = capbuff; // start out pointing to our output buffer

If you want to know the code that clears the screen, it would be:


char *cl; // clear screen sequence

cl = tgetstr("cl", &tempPtr);

If cl comes back non-NULL, you know have a pointer to the byte sequence that will clear the screen. tempPtr returns with a higher value, so when you get the next capability you use it again:


ce = tgetstr("ce", &tempPtr);

This is repeated over and over for every code you wish to send. You check for NULL to know which capabilities actually exist, so you could write functions like this:


void clearscreen()
{
if (cl==NULL)
{
printf("Sorry, I cannot clear the screen...");
} else {
tputs(cl, 1, outchar);
}
}

And now we see how these pointers get used. The tputs() function is a special output routine that handles padding (time delays for slower terminals) and other features (though it ends up writing the character out using a function you specify — such as outchar() in this example).

For the cursor movement (cm) capability, it uses a special tgoto() function that knows how to substitute the X and Y values:


void setcursor(int x, int y)
{
tputs(tgoto(cm, x, y), 1, outchar);
}

tgoto() processes the cm output string and returns one that has everything set up with the x and y coordinate in it.

By now, you may see where I cam going with this… Read the termcap entry, parse the ones you care about, then create simple functions that output the screen code sequences:

void clearscreen();
void setcursor(int x, int y);
void underlineon();
void underlineoff();

…etc…

In the next installment, I will share with you my very basic and simple 1995 code that let me convert OS-9 L2 (and MM/1 K-Windows) text programs to run under Termcap on any supported type of terminal.

And then I will explain why I decided NOT to use termcap for my current project.

To be continued…