Monday, December 1, 2008

PROP6502 Laptop Project


I made a laptop. It won honorable mention in the Parallax 2008 Propeller Design Contest. The point of the contest was to do something that shows off the unique capabilities of the Propeller microcontroller. I made a 6502-based laptop that uses the Propeller as its "chipset". I started with a toy laptop, and replaced all of the electronics with my own design to make it into a real working computer.



The screen is a 5.5 inch LCD TV for the laptop screen. The video decoder board, which was piggybacked on the LCD, made too tall a stack to fit inside the screen. After watching this presentation about modding by Benjamin Heckendorn, I decided not to try to make the laptop thicker. He gives the advice that you have to pick a height constraint and stick to it or else your project will grow to be "the Marvel Mystery House." Instead I figured out a way to unfolded the TV circuit board flat and place it below (not behind) the screen. Unfortunately that makes the LCD sit very high up the lid, so I made a nice decorative faceplate to distract the eye from how high and small the screen is.

The battery holder is embedded face up in the bottom so I wouldn't have to fashion a battery cover; the lid keeps the batteries in place when the laptop is closed.



Inside the laptop: at the top of the picture you see the back of the keyboard. In the second row, components from left to right: the keyboard decoder, back of the battery holder, and the CPU board. (More on the CPU board in a minute.) This toy laptop had a QWERTY keyboard, but it was not PS/2. Decoding this key matrix with the Propeller would be a waste of I/O pins. I realized that all keyboards are key matrices, including standard computer keyboards. So I simply cut the circuit board out of a real PS/2 keyboard from a Dell PC, and soldered the toy keyboard to the PS/2 board! The fact that that actually works (it does work!) is hilarious. Of course the key codes generated will be different; for instance pressing "A" might make the PS/2 board think you are pressing "X", but I simply translate the keycodes in software.



The laptop is based on a 6502 processor. I used the Propeller microcontroller as the "chipset" for the computer. The Propeller provides video, I/O, and memory control. The 6502 addresses a 64K static RAM chip, and the Propeller manipulates the control signals in order to load the initial program and monitor the address and data buses. Reducing chip count was key to making this board fit inside the laptop shell.



The reason I was able to do it with so few chips is a combination of the Propeller microcontroller's versatility, and a neat timing trick that allowed me to multiplex the data bus. After reading how old 6502 computers interleaved video access with processor accesses on the same bus, I realized I could use the same technique to allow the Propeller to access the bus in the interim period between each 6502 bus cycle. Since the 6502 puts the address out before the data is read or written, the Propeller can quickly transfer the 16 bit address onto the 8 bit data bus in two transfers, and still finish in time to return the data bus to a "normal" state so that the 6502 never knows anything happened.

What I learned from this project is that pins and signals are ways of dividing up the physical dimensions, but you can also think of time as a dimension that can be sliced up and parceled out. Making use of this extra dimension in a design can allow you to fold a complex system into a smaller physical space than it would seem to fit.



Once you've pared down your design, you've still got to wire it, and even a simple computer involves a nightmare of wiring. This part is harder than choosing the right chips to wire in the first place!

In a closely packed board, you can have a problem wiring it just from the sheer mass of copper packed into each square inch. I used strips cut from 80 conductor IDE ribbon cables to wire up the board. (Credit to Benjamin Heckendorn for giving this advice as well, in the same presentation I mentioned above.) These extremely thin IDE cable wires allowed me to pack more wiring into a smaller space, and it makes the wiring neater when you can group 4 or 8 together into ribbon cables.



This project gave me an excuse to finally use my Fluke 9010A Microcomputer Troubleshooter (pictured, front and center). My friend and former teacher, Earl Martin, once told me there were only ever two pieces of electronics equipment he wanted his whole life. One is a multichannel logic analyzer; the other is this Fluke 9010A, which he gave to me.


What this unit allows you to do is plug in to the CPU socket of a computer and exercise control of any aspect of the computer. You can test bus signals, read/write memory, even run programs and break on conditions. Technically it's an in-circuit emulator, but that term doesn't begin to cover the depth of what this tool can do. I would not have even attempted this project without it.



I had hoped to get better than honorable mention, but at least I'm on the same web page with the other winners, and received a $100 prize. I took the prize payment in the form of $100 of Parallax electronics components. I used the prize from this contest to buy parts for my project for next year's Propeller design contest: Norbert 2.0, a Propeller-controlled mobile Lego robot with a 5-degree of freedom manipulator arm. See you next time!

Monday, September 1, 2008

Success at Last: Wireless on the Freerunner with Om2008

Since I lose the USB network device when the Freerunner powers down, it would be helpful if I set up Ubuntu to automatically reconfigure the device when it does come back online. I haven't tested if that would unstick a frozen SSH session, but it might. Unfortunately I read Ubuntu has a bug that makes the reconnect not work.

The fix isn't exactly hard but before I did that, I decided to give my Freerunner's wifi configuration another try. It turns out the reason I couldn't get wifi to work before was due to a really silly thing. In the new keyboard app in Om2008, it shows what word it thinks you're typing, but it doesn't actually enter it unless you tap on the word. Whenever I tried to enter my WPA password, I would see the word suggestion from the keyboard app right below the blank password textbox, and I thought I was typing in the password box. Actually I hadn't entered anything yet because I didn't click on the word. Once I figured that out, I was able to connect to my wifi network.

But how to be sure that I was connected? I hit upon the idea of going into my router configuration to see if it had assigned a DHCP address to my Freerunner. Sure enough, there it was, and now I knew what IP to type in to ssh to it. Having wifi working solves some of my problems I had with USB networking. Even though my ssh session still freezes when the Freerunner goes into standby, the ssh session also unfreezes when I wake the phone back up, so I can continue where I left off while working. Still trying to figure out how to fix the shutdown problem.

Once you have wifi connected, you should be able to ping internet IP addresses, but DNS doesn't work yet because the Freerunner doesn't know your DNS server. I followed the instructions from the USB networking page to set up DNS, using the address of my router, as that part of the instructions works equally well for wifi networking as it does for USB networking.

Once DNS works, then the package manager works. I still haven't figured out the suspend problem.

Update: I must have missed that in the known issues for Om2008, they said that the builds from the last days of August (which I'm using) have to have the X screensaver disabled. The fix is to run the command "xset s off". What they didn't mention (and since it's a wiki, I was able to fix it for them :) ) is that you can't run xset from the ssh session, you have to install the terminal app (after getting DNS working) and run the command on the phone itself.

Sunday, August 31, 2008

USB Networking on the Neo Freerunner

Well the new version of the open moko software isn't much better; they changed a lot in the interface but it still has some serious bugs.

This version of the software has a bug that causes my phone to suspend every 30 seconds when the USB cable is connected, even if you have suspend set to "disabled". Guys you have GOT to fix these bugs! Seriously. I was honestly better off before I upgraded, in terms of reliability.

In order to do much with the Neo Freerunner, you really have to have USB networking set up. I found some instructions that work nicely on Ubuntu. However, if the phone suspends at any time during the process (as mine does constantly due to the bug I was talking about), then -poof- your USB device magically goes away and you lose your ifconfig settings or crash your ssh session!

Unfortunately you have to have USB networking set up in order to run the install scripts to put Debian onto the SD card. Maybe I can type in a command that will force it not to suspend, but I have to do it faster than thirty seconds over SSH because it looks like they took the terminal app out of the newer release. (The one thing that was actually any good in the old release, and they take it out? WTF?)

I'm almost ready to flash Qtopia over the open moko software and forget about it, but I did really want to see what apps the open moko distribution would have available if I could get networking so the package manager will work.

My Neo Freerunner Arrives



It's here! It's finally here! My Secret Decoder Ring Neo Freerunner!




When you open the box, you're greeted by a quote from the Tao De Ching. I keep that same book on my desk. How cool is that?




The quote seems to be about the empty hole in the bottom of the phone. Funny, I figured that was where the rope goes.




Here's what you get in the box: a stylus, USB cable, a charger with plugs for various countries, and a 512MB SD card. The back side of the green card says "Thank you for your efforts to Free The Phone."




The Neo Freerunner comes with the coolest stylus I've ever seen. On the top side of the device is a laser pointer and a bright LED flashlight. The bottom of the device has a clever mechanism which when twisted one way extends a plastic stylus tip, but when twisted the other way reveals a ballpoint pen tip. Not quite a Sonic Screwdriver but close! I ended up using the flashlight on this thing just a few hours later when a thunderstorm knocked our light out.




The display on the phone is really quite beautiful.

Getting the OpenMoko

As our cell phone contracts expire soon, I wanted to take the opportunity to get a phone that I can hack the software on (I've been quite frustrated by the horrible software on my Sprint phone). I've wanted an OpenMoko Freerunner phone because it runs Linux and you have true freedom to change the software on the phone as you like, but I wasn't sure if I could convince my wife we should spend money on it. She surprised me, however, by promising me one for my birthday!

Actually buying an OpenMoko Freerunner turns out to be a bit difficult. The "Store" section of the openmoko site simply showed "Sold Out" with a claim that more phones would be coming in July, but it was already August and they hadn't updated that message. You have to go through one of their 3rd-party distributors. There's a catch: the only US 3rd-party distributor (GP2X) doesn't actually sell the latest version of the phone!! They are selling the Neo 1973, which is a prototype to the Neo Freerunner. Due to the similar sounding names, it would be easy to accidently buy the older hardware, which lacks some features and already isn't always supported in newer software releases. Read carefully before you buy!

As of today when I look on the site, it seems you can now buy the new phones directly from OpenMoko, but when we ordered mine earlier this month, the only place that had it was the Canadian distributor, Koolu. Having never bought anything from another country before, my wife and I were unsure how to pay for something in Canadian dollars with a US checking account, but it turns out the exchange rate is all handled automatically. We didn't have any problems with the Canadian company, Koolu, but we had a string of problems trying to go through the US company PayPal to get the phone paid for. First PayPal froze my wife's PayPal account because they flagged it as suspicious activity (because it was my wife's name on the account but the ship to was my name). After jumping through a bunch of arbitrary hoops to get my wife's PayPal account unlocked, PayPal dropped the transaction anyway because too much time had passed. So I ended up buying the phone on my account and PayPal ran it as an echeck instead of a credit card, which is not what I wanted. It turns out if you are PayPal customer, there is no way to tell PayPal to run it as credit, even though if you are not a PayPal customer, PayPal will run it as credit! What kind of crap company denies a service to their members that they will give to nonmembers? So I canceled the original transaction and re-ran it as credit and not as a PayPal member, and finally got the phone. However, instead of refunding my first transaction, PayPal double charged me, wiping out my bank account. The funds PayPal sucked from my bank account were there in my PayPal account, true, but I had only intended to pass money through PayPal, not deposit it there. PayPal wasn't going to return my money from the canceled transaction unless I specifically asked for it back. As long as I don't notice what they did, PayPal effectively had an interest-free loan from me. Bastards.

Monday, August 25, 2008

I Feel Stupid And Triumphant

For years I haven't been able to run Linux on my laptop because my wireless card is not supported on Ubuntu. Dell used several different Broadcom chipsets in different runs of the laptop I got, and I had the bad luck to get the ONE chipset that is completely unsupported on Linux. The model stepping BEFORE this wireless card works, and the one after, but this one just proved to be too tough a nut to crack for the Linux driver people. By a ridiculous stroke of bad luck it ALSO has a Windows driver which can't be used with the NDIS wrapper. Trust me I've tried everything; many sleepless nights to no avail.

But in an unrelated project, I've been playing with these WRAP PC Engines router boards that I got for free because they were lightning damaged. A couple of them had mini-PCI wireless cards attached to them. These cards use Atheros chipsets, which are the most well-supported wireless chips under Linux. They would fit in my laptop, but I didn't think they worked because I could never get them to work in any of the WRAP boards. Plus I imagined tearing down the laptop to find the wireless card inside it would be a pain in the butt, so I put off actually swapping in a card to check if it worked.

When I upgraded the RAM in my laptop today I found out that the wireless card is not hidden in the bowels of the machine; it's just located under the same access panel with the RAM! I also had had the WRAP boards out and I noticed that the Atheros wireless cards I had weren't just crappy wireless "B" cards, they were really wireless "G". So I finally swapped one in and it worked! I'm writing this post from Ubuntu Linux now on my laptop, wirelessly! Yipee!

Then I realized that I could have done this six months ago. Now I feel stupid.

Sunday, August 24, 2008

C++ Iterators

A friend of mine and I are collaborating on a game in the C++ programming language. The part I'm writing is a general purpose engine that will be used on both the game server and the game client, so I decided to use templates to allow basic game objects to be replaced with entirely different classes in the client and server, yet still use all the same code on top of them.

In the process of moving my code from regular classes to template classes, I uncovered a fundamental language wart or compiler flaw. It has to do with a problem I ran into using STL iterators. Before I go into what the problem is, let me explain iterators a bit.

The people who wrote C++'s standard template library want to break you of the habit of writing things like:

for (int i=0; i < vec.size(); i++)

Instead you should write this:

for (vector< int >::iterator i=vec.begin(); i != vec.end(); i++)

Do you see the improvement? No? It's supposed to simplify code for traversing containers--except that it's twice as much typing! OK, well it also gives your code portability to use different containers besides vector ...theoretically... except that we have to specify "vector" directly in the for loop to say what kind of iterator it is! Oops.

The real motivation behind iterators is that it unifies the concepts for the people who write the container classes. I won't say the users of the container classes can't use iterators in ways that also provide benefit, but the users also bear a burden of extra boilerplate that adds as much work as it saves. This is the sort of thing Bjarne Stroustroup means when he talks about the compiler vendors and library/tools group being overrepresented on the C++ standards committee compared to actual users of C++. At least we users are finally going to get the auto keyword in C++0x, which will allow us to just write the word "auto" in place of the vector::iterator gobbledygook.

But being the good little C++ citizen that I am, I've taken the trouble to use iterators in all my code, even if it is a lot more typing. That's how when I moved my engine definition into templates, I got burned badly.

Where before I had this:

for (vector< GameObj >::iterator i=vec.begin(); i != vec.end(); i++)

I now have:

template < typename T >
... ...
for (vector< T >::iterator i=vec.begin(); i != vec.end(); i++)

What is supposed to happen is that when I instantiate the code with T=GameObj or T=OtherGameObj, the code will get built at the point I instantiated it and the result of the latter will look like the former except with either GameObj or OtherGameObj substituted for T.

Instead, it turns out that you can't (at least on my compiler) use a template argument like T to specify the type argument to a container iterator. You can declare the container using that T, but you cannot access the iterator of the container you just created! On some containers like vector you can just go back to the old notation, but for containers like map<>, iterators are the only way to loop over the container. This is a Really F*ing Big Problem. This is like laying the foundation for a building and realizing you didn't leave room in the floor plan for any doors. The irony is that iterators from the standard template library don't work with template arguments! This is like forgetting to leave room for doors in the plans for a f*ing door factory!