We used to make things.

I remember a time when we used to make things. Duvac in Pasadena used to carry wire wrap supplies and integrated circuits. Fry’s used to have a wide selection of products for making circuits that weren’t contained in packaging that yellowed with age. You could buy Nixie displays from Radio Shack.

Perhaps it was a matter of economics: if you wanted a home computer on the cheap you needed to make it yourself. Perhaps it was a matter of tinkering: back then, you had to build your own stuff, while today “tinkering” involves playing with PHP and building your own web site. Or perhaps it’s simply just a matter of a culture which wants things faster, better, cheaper–and when you can buy a really cool computer or GPS gizmo with full color display for under a couple of hundred bucks, who wants to spend a thousand to get a small box with a handful of LEDs that flash?

But no-one seems to make things anymore. Which is a shame.

So in my spare time I decided to build a Simon-like game clone–because I can.

Here’s the circuit diagram: game circuit diagram, and here’s the source kit for SDCC for the software for the game: game source kit. The final ROM size is 1193 bytes, well under the 4K limit for the HC08QY4. (With some work it probably could be made to fit in less than 1K, suitable for the HC08QY1, which in quantity is around $1/unit.)

Update: There is a typo in the diagram. The CPU used is the MC68HC908QY4, not the QT4. The QY4 is the 16-pin version, and what is shown is the 16-pin DIP version of the circuit. The QT4 is the 8-pin version of the same processor.

Here are some pictures, including the box I made to hold the game:


And here’s a video of the game in action.

The source files uses SDCC for compiling the source kit from C to assembly language. There were a few gotchas that occurred while making the game:

(1) While the SDCC documentation says if you use the __interrupt(N) keyword an appropriate interrupt entry will be entered into the interrupt table of the HC08 processor, I found that for some reason the entry wasn’t added after all. Thus, the interrupts.s file actually provides the correct entries into the interrupt table. If you add a new interrupt, you should then update the interrupts.s file to reflect the new interrupt.

(2) I use stack variables rather than globals; this is because the underlying architecture is interrupt driven. In events.c I use the timer interrupt (configured to interrupt every 1/100th of a second) to poll the keyboard and generate key up/key down events, as well as fire off an event. This architecture is perhaps a little overkill, but it does allow me to respond to very complex logic without worrying if the logic fits in a 1/100th of a second window. Further, this has the advantage that in my GetNextEvent() routine, if there are no events taking place, I can place the processor into a low-power WAIT state, which reduces considerably the amount of power drawn by the device.

The overall advantage of this is that the game runs just fine on a couple of AA batteries. And I expect that, during game play when no lights are turned on, the total draw of the circuit should be on the order of 1.1mA. By contrast, the biggest draw are the LEDs.

I love the interrupt-driven architecture, by the way: if the same architecture were to wait for key strokes using the keyboard interrupt architecture–for example, we were using this chip for a keypad for a combination lock–we could put the processor into STOP mode. Without any external circuits being powered, in STOP mode the QT4 draws 0.36µA of power–meaning it could run in this standby mode on a couple of AAs for nearly 22 months.

The stack-based variables, however, is so I can keep everything straight, since in theory any routine could be re-entrant.

But this means that many of the built-in run-time libraries for the HC08 on SDCC don’t seem to work right. My random number generator deals with the reality that in-line long integer multiply drops to a library call–so it doesn’t work right at all. However, a long-integer shift does work correctly, so I implement the GNU random() number generator algorithm using shifts and adds in C, which then generates the right (non-library calling) routine.

(3) There is no sound. That’s because I didn’t have access to a small speaker. I intend to fix this with a future version when I have some time. When I do this, I’ll probably use the same interrupt loop (slightly tweaked) to drive the speaker to seven different sounds: one for each position, and an “you fail” sound. (One possibility would be to tweak the interrupt timer duration and adjust all the timing of the rest of the game to match. Another would be to up the interrupt timer frequency, and maintain two additional counts: one which indicates if I pull up or down the speaker line, and another which indicates if I should drop into my old 100Hz interrupt routine.)

(4) The animations are perhaps a little over the top. With sound I probably could simplify the startup sequence. I also wonder if I shouldn’t add a seventh ‘start’ button to start the game instead of using the “hit any button to start” method I’m using now.

(5) Why are the lights so strange, with D1 being the lower right LED? Because I got disoriented when I was hooking up the LEDs: my intention was to make D1 the top light, with other lights working clockwise around. Instead, I wired up LED 6 as the lower light, with the other lights working counter-clockwise–meh. It was easier to rework the software than re-glue the lights. I worked the switches in the same order deliberately so I didn’t get confused.

Sure, it’s a silly little gizmo with a handful of lights. But I had fun!

Why the App Store is cool and why other app stores would also be cool.

I just spent a half hour on the phone with one of our VC partners sorting out how to get our iPhone application on his phone. Turns out the problem was that he was using Outlook Web, and the provisioning profile e-mailed to him was being corrupted by the web.

It’s one of the things I love about the App Store: all of the moving parts of shipping someone an application is reduced to two: the App Store server and iTunes. It eliminates the potential problems of file corruption and validation by replacing all of the potential problems of file transport and installation steps to “push this button, sync that device.”

Lesson 1: Always zip the provisioning profile. If there is a file corruption issue, the unzip process will detect this.

Lesson 2: If I ever get the chance to architect a simplified installation process via the Internet (such as, for example, using Java Web Start), use that instead of some ad-hoc mechanism for installing my application. Because saying “click on this link and click the OK button” is a hell of a lot easier than “here’s two files; drag this there, drag that there, clear this cache, reset that thing, plug in and hit ‘sync'”.

A protest in code.

In response to my earlier comment about Peeps being rejected for using a private API when they in fact rolled their own implementation of CoverFlow is to create my own implementation of CoverFlow.

FlowCover is licensed under a BSD-style license; feel free to use the code as a basis for your own iPhone application. There is some documentation as well.

I’m curious as to what Apple will do. It seems unreasonable for Apple to reject an application because it implements a user interface that looks like Apple’s other user interfaces on the grounds of “using a private API” while Google gets away with actually admitting in public of using a private API.

Which brings me to CoverFlow itself. I admit it’s a clever UI. I like CoverFlow; it makes browsing through my images really convenient. And it looks nice in iTunes. But let’s be real: implementing a CoverFlow like UI takes about an evening for someone who knows OpenGL–as I just demonstrated by spending an evening implementing a CoverFlow like UI. And I’m seeing CoverFlow-like UIs showing up in a number of places, along with discussions on how to implement something that looks like CoverFlow in other places. So closing up the CoverFlow API on the iPhone and shutting down people for independently implementing something that at best would take a few days (if you count stuff I haven’t done yet, like clicking on a cover to flip the view to a control panel) seems absurd to me.

Startups and the Incumbent Problem.

Ideally our society organizes itself around the principles of a meritocracy: we award the “winners” in our society, where by “winning” we mean that in the competition of a fair and equal capitalist market, those who are the best and brightest percolate to the top.

Of course this is the ideal. But in such a huge work force, local factors prevent the best and brightest from percolating up, and local factors entrench the less worthy at the higher echelons. For example, think of a boss who has political connections within an organization entrenches himself and promotes the “yes” men while the people who make valuable contributions to the bottom line are pushed out as threats to the corporate power structure. While it is true that in the long run such inefficiencies would lead to the “creative destruction” of that company, as top-heavy structures built around inefficiencies allowed that corporation to be overrun by startups–including those potentially founded by the workers who were pushed out by that corrupt boss, “creative destruction” also has a negative social effect as other workers who did an excellent job lose their positions because of a boss who doesn’t uphold his fiduciary responsibility to the company.

One interesting thing I’m now watching first hand is the incumbent problem at a startup company I’m now working for.

In this slow economic environment we just got our ‘A’ round of financing, so now we’re in a position to start hiring people. And because the economy is very slow I’m seeing a few excellent candidates come through the door–people who, in my opinion, could potentially do a better job than many of the incumbents that already hold a job at our little startup. And here’s the problem: how do you balance the risk taking of less competent incumbents with the needs of the company to hire the best and brightest? After all, the incumbents were the ones who got us to this stage–even though they may not be the best to take us to the next stage.

I used to hear horror stories about Venture Capitalists firing the founders of a company as soon as the company starts taking off, and I used to look at it from the point of view of the founder–someone I could sympathize with. After all, someday I wanted to start a company, and it’d be terrible if I took all that risk, only to have my dreams and ideas taken by someone with a deep checkbook.

But now I’m watching it from the other side–from the side of a Venture Capitalist who is investing a lot of money into a company and who wants to maximize their return. And sadly, it could very well be that the founder who gets tossed off the company he founded may not be the worst thing for a company–since, quite honestly, the primary purpose of a company is not to entrench yourself with your buddies, but to make a profit. And with a VC-funded company, to make enough money to give a decent return on the high risk investment the VC is making.

I sort of get the feeling that’s one of the primary reasons why a startup fails: because the incumbents don’t know how to deal with the company they’re starting, but they won’t step out of the way to allow knowledgeable people step up to the plate.

Why good user interface design is important.

Good design represents a visual “language” of sorts: when deciding upon the elements of your interface, your book, your map, you are designing the language that you are using to communicate with your reader or user. You are, in other words, giving the subtle cues which say “stop here”, “this is important”, or “this is what you need to do next.” Subtle cues can also indicate things like “I’m doing something in the background” (the rotating network busy icon in the top of the iPhone) or “You may not be able to do that here” (the status bars indicating signal strength).

Design can also save a life. Literally.

A friend of mine was telling me a story of a middleware company that almost killed someone. Their software is used in the medical profession; one element of the software would print the paperwork a pharmacy uses to fill a prescription. And the problem was that one elderly gentleman got a bottle not met for him because in the dozen or so bottles to be filled, the workers at the pharmacy accidently added someone else’s prescription to the stack of pills intended for that elderly fellow.

While he blamed it on user error, the reality is that this is a failure in good design: all it would have taken was a cover sheet between prescriptions to serve as markers between prescriptions intended for a single recipient. Or (since they provided the hardware) a specialized printer which can collate and staple automatically. Or even redesigning the page so that multiple prescriptions are fit on a single page as much as possible, with a large (and easy to read) page number and total number of pages in one corner, along with a count indicating the total number of pills to fill for this individual.

Any of these visual cues would have made it easier for a worker to notice something was wrong: as soon as the prescription count resets to 1 or the page count resets to 1 or he hits a cover page, he’d know that maybe he should read the patients name a little more carefully for this batch of pages, rather than shove potential lethal poison at an elderly individual who doesn’t know better.

The Mortgage Crisis.

When I started this blog, my intent was to limit my discussions to computer development and technical topics. However, in another life I’m also deeply fascinated by politics and economics, and I’m also fascinated by development, especially housing development issues. And today’s mortgage crisis is a perfect storm of problems that all land in my other areas of fascination.

So please excuse me while I indulge myself.

An overview of the Mortgage Crisis.

One of the most readable articles I’ve seen on the mortgage crisis comes from The Institute of Chartered Accounts in Australia: The collapse of the US sub-prime mortgage market.

This report does an amazing job describing all of the players in the U.S. mortgage market, describing the process of a mortgage and mortgage securitization through a set of fictitious players. Page 8 of that report gives a diagram of all of the players in the mortgage market, and later in the report (if you are an accounting geek, which I am thankfully not), a list of issues in U.S. Generally Accepted Accounting Practices which our mortgage crisis illustrates, such as the practice by GAAP to allow banks (such as “Insecurity Investment Bank” on page 8) to show on paper that it has no involvement (and thus no risk) when it is neck deep mediating payments on a CBO.

But I digress.

One thing the article does not describe, though it is relevant here, is the role that Freddie Mac and Fannie Mae play in the mortgage market.

On page 8, Freddie and Fannie play the role of “Insecurity Investment Bank” for conforming sub-prime mortgages. Back in 2003 as a result of an accounting scandal, Freddie and Fannie were given the additional mandate to increase its purchases of sub-prime mortgages to 50% of its overall purchases, in order to encourage low-income families to qualify for home loans.

This increased purchasing requirement created a market distortion, encouraging the other players in the diagram on page 8 to act in ways that would otherwise not be in their best economic interest. Because Freddie and Fannie were funding a greater pool of sub-prime mortgages, banks (“Last Bank & Trust” in the diagram) were encouraged to make more and more sub-prime mortgages safe in the knowledge that Freddie and Fannie would buy the loans. And because Freddie and Fannie were government sponsored enterprises chartered by the U.S. government, both bond insurers (such as AIG) and investors (“SPE Cayman Islands” in the diagram) insured and purchased the CDOs created by Fannie and Freddie thinking that these bonds were insured by the full faith and credit of the United States Government.

When some economists and politicians accuse the government of being in the middle of this whole mess, when you look at the diagram, you can see that in fact Freddie and Fannie were in the middle of this whole mess. The problem was not “deregulation” or “fat cats” preying on poor folks: if you dig through the discussion on the U.S. GAAP practices (which are driven by our tax code, the SEC, and regulations such as Sarbanes-Oxley), it should be clear that each actor in our economic disaster were doing what they believed to be legally correct–though part of the problem here was a lack of understanding by each of the players of the overall risk that each was exposed to.

But the basic problem was this: the bubble in the housing market starting earlier this decade hid the poor quality of loans being made. Freddie and Fannie could continue to drive the sub-prime mortgage market, and the banks and investors and insurance companies could continue to make money believing that the government backed the entire process.

While it is unclear the Freddie and Fannie’s “free money” drove housing prices up, it could not have helped the process: bubbles are often driven by “free money” being irrationally allocated. But it is clear that once that housing bubble popped, the entire market collapsed, leaving us the mess we have today.

Freddie and Fannie’s irrationality.

So why did Freddie Mac and Fannie Mae act against their own economic self-interest? Freddie and Fannie are GSEs, certainly–but that means that while their direction may be set by congress, they are responsible for showing a profit. Until a few months ago when they were effectively nationalized, they did not operate using taxpayer money.

Well, that all goes back to 2004, when the U.S. Department of Housing and Urban Development required Freddie and Fannie to purchase more “affordable” housing loans: How HUD Mortgage Policy Fed The Crisis.

Since HUD became their regulator in 1992, Fannie and Freddie each year are supposed to buy a portion of “affordable” mortgages made to underserved borrowers. Every four years, HUD reviews the goals to adapt to market changes.

In 2004, HUD reset the goal to 56% of overall purchases: that is, HUD required Freddie and Fannie to make 56% of its overall purchases in the sub-prime mortgage market in order to encourage poorer people to buy homes.

Unfortunately, as it turns out, those same poor people couldn’t afford the homes when the teaser rates started to adjust.

Politics: where the rubber meets the road

So one has to ask oneself why was it that banks stopped acting in their own economic self-interest? Even with Freddie and Fannie beating the bushes for sub-prime loans, they were not the only Investment Banks repackaging those mortgages. Part of the problem lands squarely on the Community Reinvestment Act, a law which nominally was supposed to end the practice of “red-lining” low-income areas. The problem is the act essentially created a “quota system” whereby banks operating in a given area had to show that their investment portfolio (including their mortgage portfolio) matched the region’s socio-economic profile.

On page 8 of the Chartered Accountants report, the CRA regulations directly affected the “Last Bank & Trust” in the diagram: it strongly encouraged Last Bank to make its loan to “Mr. & Mrs. Jones”, in order to meet Last Bank’s CRA quota requirements. The fact that the bank could make a buck off the transaction was a bonus–but even so, Last Bank was in the position that if they didn’t make these loans (which were not in the bank’s best economic interests), they could face protesters from various local community groups demanding greater CRA compliance.

And who took advantage of the CRA to force more sub-prime loans into poorer neighborhoods?

Spreading the Virus

Yet ACORN had only just begun. Two days later, 50 to 100 of the same protesters hit their main target – a House Banking subcommittee considering changes to the Community Reinvestment Act, a law that allows groups like ACORN to force banks into making high-risk loans to low-credit customers.

The CRA’s ostensible purpose is to prevent banks from discriminating against minorities. But Rep. Marge Roukema (R-NJ), who chaired the subcommittee, was worried that charges of discrimination had become an excuse for lowering credit standards. She warned that new, Democrat-proposed CRA regulations could amount to an illegal quota system.

FOR years, ACORN had combined manipulation of the CRA with intimidation-protest tactics to force banks to lower credit standards. Its crusade, with help from Democrats in Congress, to push these high-risk “subprime” loans on banks is at the root of today’s economic meltdown.

ACORN (Association for Community Organizations for Reform Now) is an umbrella organization whose goal is to make affordable housing available to poorer people. While their goals are completely laudable, like any powerful organization they had friends in Congress (largely Democratic) and their voter registration arm operates like any other special interest group, gaining votes for their congressional supporters so they can get legislation passed to support their own point of view. As such they are no different than any other organization such as the National Rifle Association.

And like any relatively sophisticated organization, when they ran into problems on the ground with insufficient funding of low-mortgage homes, they sought to resolve the problem in the system where they could:

As ACORN ran its campaigns against local banks, it quickly hit a roadblock. Banks would tell ACORN they could afford to reduce their credit standards by only a little – since Fannie Mae and Freddie Mac, the federal mortgage giants, refused to buy up those risky loans for sale on the “secondary market.”

That is, the CRA wasn’t enough. Unless Fannie and Freddie were willing to relax their credit standards as well, local banks would never make home loans to customers with bad credit histories or with too little money for a downpayment.

So ACORN’s Democratic friends in Congress moved to force Fannie Mae and Freddie Mac to dispense with normal credit standards. Throughout the early ’90s, they imposed ever-increasing subprime-lending quotas on Fannie and Freddie.

The Housing Bubble and the Great Unraveling.

In normal economic times, of course, all this repositioning, while creating a market distorting effect, worked fairly well: lower-income families were able to afford better housing, local communities improved, and the process of “gentrification” started to raise the standard of living for poorer families, especially in inner city areas the CRA was designed to help.

But when the housing price bubble started, the systems that were put in place to allow lower-income families to afford better housing kicked into high gear and created more and more bad sub-prime loans. Freddie and Fannie set standards for ARM mortgages–but to qualify a home owner only had to qualify for the fixed part of an ARM adjustable mortgage. Banks were required to meet CRA targets, which in 2004 were reset ever higher, which means they had to get more “creative” to meet those quotas–and Freddie and Fannie were there providing the funds. Investors bought more and more sub-prime loan CBOs believing they were backed by the full faith and credit of the United States.

And rising house prices were an economic price signal suggesting that the high cost of housing in areas like New York and California (driven by a genuine shortage of housing, in turn driven by high populations) was spreading to the rest of the country: the old rule that the average price of a house is about 3 times greater than average income in a local area went out the window.

Economic bubbles don’t last forever. And when this one collapsed, all the players were stuck holding the bag–including foreign banks who bought CBOs and, ironically, were some of the first players to collapse. (For example, Northern Rock PLC in England failed in 2006.)

But to suggest that this was a failure of deregulation is insane: even without regulations banks will not operate against their best economic interests, unless there are market-distorting reasons to do so. And in this case, Freddie Mac and Fannie Mae, combined with the CRA’s quota requirements to loan into poorer neighborhoods, masked (and partially driven) by higher home prices, created the overall disaster.

And when housing prices popped, the entire row of dominos, set in place by organizations like ACORN who was seeking affordable housing for the poor, by Freddie Mac and Fannie Mae looking to fund that affordable housing, and by laws such as CRA which sought to institutionalize sub-prime mortgages in the name of social justice, fell.

The lifespan of an object owned by NSMutableArray, or why Objective-C annoys me and Java makes me happy.

Hard to find memory bug on the iPhone.

I had to find it by (a) building the map file for the distribution of the iPhone product, and (b) walking for an hour around Santa Monica (pretty day, though a bit on the warm side) waiting for the map software component to crash.

I finally found it. It’s the sort of memory bug that Java programmers never have to concern themselves with.

So suppose you’re writing a queue, and you use an NSMutableArray. (In Java you’d use a LinkedList object, which would be faster, but I digress.) Removing the object from the NSMutableArray, I wrote:

MyObject *obj = [myMutableArray objectAtIndex:0];
[myMutableArray removeObjectAtIndex:0];
UseMyObject(obj);

And all is right in the world, correct?

Um, not quite.

See, there is thing thing called the autorelease pool. It’s quite clever: it holds a list of all the objects that, after this current event is finished being processed, the objects will be released and potentially deleted from the system. So when you get a return value back from some function, unless its a function which explicitly gives you ownership of the object, you can safely use that object until the end of the event processing loop, which certainly won’t happen until after your routine returns, right?

Um, not quite.

NSMutableArray notes in its documentation that on a call to the remove function removing that item from the array, release is called immediately on the object, which means my object obj above has been released. Now what makes this a hard to find bug is that while the object may have been deleted and its memory released, the object itself still lingers in memory unless you use a memory debugging library.

And unfortunately for me (1) the conditions causing this routine to be called is only triggered when walking around with our new software package, and (2) the various memory debugging libraries only work under the iPhone simulator.

The workaround?

MyObject *obj = [myMutableArray objectAtIndex:0];
[[obj retain] autorelease];  // before removeObjectAtIndex
[myMutableArray removeObjectAtIndex:0];
UseMyObject(obj);

Feh.

This is exactly the sort of stuff that garbage collection helps with, and is the primary reason why I like Java: no messing with object lifecycle management. Unfortunately garbage collected Objective C isn’t available on the iPhone, which means that so long as I’m trying to write Objective C code that runs on the iPhone and may also run on the Macintosh, I cannot use garbage collection in my Objective C development.

Four ways of using a mobile phone.

I’m starting to conclude there are four ways a user will interact with a phone. These four modes or four methods are:

(1) Checking incoming messages or current device status. This should be very quick and unobtrusive: you should be able to just press a button or two and get the status you want.

(2) Sending a message. This should also be relatively quick, though often people are willing to spend time composing messages, such as SMS messages or quick e-mail messages. And in this category I’d put chatting away on the phone: you’re involved with the device communicating to someone else.

(3) Searching for something. This may wind up being involved, but ideally this should be somewhat location-based for some types of searches.

(4) Playing games. You’re sitting around for a few minutes killing time, and you want a way to pass that time.

Some things that sound like they should fall into one category really fall into another: surfing the ‘net on the iPhone, for example, strikes me as an activity similar to playing games–a way to kill time. Other activities may also sort of fall into multiple categories: for example, if you search for a movie theater to see what’s playing, is further followups to that theater a ‘search’ activity or a ‘status’ activity? (In other words, would you want to revisit the movie theater to see what’s playing on a different day, or do you want to be alerted 20 minutes before a movie at that theater starts so you remember to wander back to catch the movie?)

I think one of the mistakes people are making with respect to the phone is that they are treating it as a desktop computer, rather than as a sophisticated communications device. More than once I’ve seen people try to suggest the iPhone become an ‘immersive’ environment. How immersive can a 5″ diagonal screen be?