Thursday, May 24, 2012

Programming Style

The mark of a good computer programmer is one who A) has a coding style and B) is consistent in its use.  But programming styles are almost as unique as the developer using it.  This becomes a pain when you're working on a project with other developers, or when you're copying code from the Internet.  Anyway, the main purpose of this post is to talk about two main coding styles.

Style 1 - Old-School
I don't know the name (assuming there is one) for this first style, so I'm going to call it "old-school" (more on that later).  In this style variables and function names are all lowercase, and the underscore (_) is used in place of spaces.  A typical block of code might look like this.

int string_length(char *input_string)
{
    int string_length = 0;

    if(input_string != NULL && *input_string != '\0')
        string_length = calc_length(input_string);

    return string_length;
}


Style 2 - Hungarian Notation with CamelCase
The second common style is a combination of Hungarian notation with CamelCase.  This means variables are prefixed with the type of variable.  Also variables and functions have the first letter of each word capitalized and don't use spaces.  In this second style the above function would look like this:

int StringLength(char *szInput)
{
    int nStringLength = 0;

    if(szInput != NULL && *szInput != '\0')
        nStringLength = CalcLength(szInput);

    return nStringLength;
}



Me personally, I greatly prefer the second coding-style.  It's easier to read (although that's largely due to the fact that I've been coding in this style for a decade).  It also is easier to type.  This style tends to be less wordy, plus it has fewer underscores which isn't the most convenient key stroke combination to type over and over.  Finally, I think it makes for better coding and debugging.  It helps to look at a variable and at a glance know what type it is.  If I see a variable "customer_number" I don't know, is it a plain number or is it a string representation of a number - which makes a huge difference.  But if I see "strCustNum" or "nCustNum" I know which type it is.

Lastly I wanted to explain why I referred to style #1 as "old-school."  As far as I can tell, this coding style originated in the 1960s or 1970s.  It's been around a while, and I would argue has been superseded by better styles.  Another thing I've noticed, style #1 is primarily used by Unix developers whereas style #2 is primarily used by Windows developers.

Friday, May 18, 2012

Windows 8 - not ready for primetime

There has been a lot of talk in the tech world lately about the upcoming Windows 8. For a while now Microsoft has made pre-release builds available for people to download and install. I decided to give it a try (on a test machine) and see what it's all about. I downloaded Windows 8 and installed it onto a virtual machine. Within 5 seconds I got this.
When I saw this I had a lot of different things go through my head:

  1. Is this a new BSOD (Bliue Screen of Death)?  If so, at least they kept it blue.
  2. Did Windows 8 seriously crash before I could even fully load the installer?
  3. I thought the Windows 8 preview was designed for and tested on virtual machines.  Heck, I'm using Microsoft's own Virtual PC 2007.
  4. Are you serious Microsoft - you used an emoticon in the error dialog?  I've come to expect a certain level of professionalism from Microsoft and emoticons in error dialogs is not part of it.
  5. Continuing the unprofessional theme, there are two contractions in the screen above.  As a general rule you should avoid contractions in computers and other technical documents.
I may try again on another machine, but my initial impressions are Windows 8 is a long way from being ready.

Friday, April 20, 2012

Daytime running lights

I just have to say, I hate daytime running lights (DRL) on cars. Or more accurately, I hate automatic daytime running lights. Why you ask?


  1. The thing that annoys me the most is the fact that you have no control over your headlights. It's not very often, but there are times when I want to turn off my headlights, but with automatic DRL I can't. There's one family member, when we visit them we like to sneak up to their house to surprise them. But when we show up at night it's hard to sneak up with your headlights on.
  2. Most of the time DRL is unnecessary. There are some roads where having your lights on is a good safety feature, mostly county roads. But most of the time people are driving city streets or freeways where having the headlights on does no good.
  3. It wastes energy and causes pollution. With the lights the drain on your alternator increases which in turn decreases your gas mileage. Admittedly it's not much, but it adds up. I don't know exactly, but let's guesstimate that headlights on lowers your fuel economy by 1/10th of a mile per gallon. Now let's assume the average car gets 25 miles per gallon. And finally let's assume an average of 15,000 miles per year. If you do the math this comes out to 3 extra gallons of gas per year. Again it's not much, but that's $12 in gas. Also, let's look at the big picture. There are over 250 million cars in the US. If you assume only half of those have DRL, that works out to $1.5 billion in extra gas each year. Not to mention all the extra pollution.
  4. Because the headlights turn on automatically, so some people fail to turn on their lights when driving at night. Just this evening I saw a car on the freeway. The headlights were on because of DRL, but the driver didn't turn on the vehicle lights so the tail lights were dark. Talk about safety hazard.
  5. Believe it or not, headlights on during the day can in some cases actually decrease safety. In WWII the US mounted lights on the leading edges of their fighter plans. When the plane was diving to attack a target they would turn the lights on. Because the sky behind them was bright, the lights make the plane harder to see. So if your headlights are on and you come over a hill, your vehicle against the sky behind is harder for oncoming traffic to see thanks to DRL.
  6. DRL has to wear out the bulbs faster. Any light bulb, the more you leave that light on the quicker it burns out. So with DRL surely headlights burn out faster. I don't have any proof of this, but it makes sense.


I think manufactures should stop adding DRL as a feature to new cars. My truck has it, but fortunately it can be turned off. However, my wife's car has always-on DRL. I've gone so far as to research how to disable it. While it is possible, it requires cutting wires under the dash which I'm sure would void the warranty.

Tuesday, April 17, 2012

Dehydration

Dehydration is a simple process to understand - it's literally the removal of water. But understanding the different types of dehydration can benefit you in ways you may never had realized.

There are at least 4 main ways to dehydrate something.

1. Heat
Probably the most well-known method is heat. This is how most store bought food dehydrators work. This is also how clothes and hair driers work. But this method has its downside. What if the item you're dehydrating cannot tolerate heat?

2. Airflow
You can very effectively dehydrate something with nothing more than airflow. If you've ever seen carpet that was flooded, to dry out the carpet they place large fans on the carpet. Obviously you can't put the carpet into the clothes dryer, and too much direct heat could melt the fibers. So they just use air passing over the water to dry it out. This method works best if the air is dry (low humidity). If the air is already saturated with water, it won't pick up more water. Airflow is the means by which a swamp cooler works.

3. Cool
Yes, cool, the exact opposite of heat (number 1) dehydrates as well. I can't really offer a good explanation, I just know that it does. By far the best lesson to be learned here is bread. A lot of people buy a loaf of bread and when they get home they put the loaf into the fridge to keep it from spoiling. The problem is the cool air of the fridge dehydrates the bread and makes it stale. You're better off leaving the bread on the counter in an airtight container. Of course, if left out too long (days or weeks) it will eventually grow mold. If you need long term storage put the bread into your freezer. "But wait, you said cold dehydrates, so why put it into the freezer?" Well I actually said "cool" dehydrates, not cold. The thing with the freezer is the temperature is so cold that the moisture in the bread turns to ice before it can evaporate into the air. So the short take home lesson, leave bread in a sealed container on the counter, or in a sealed container in the freezer, but never in the fridge.

4. Absorption
The final way to dehydrate is through absorption. Obviously you can use cloth and paper towels to absorb water. But you can also use substances to dry out the air which in turn dries out the "wet" object. Have you even opened something you just bought and inside was this small paper bag full of little beads? That's called a desiccant, which is just a hygroscopic substance. Hygroscopic is just a fancy work meaning it absorbs water. Common hygroscopic substances include paper fibers, sugar, honey, and table salt. Another really good hygroscopic substance is uncooked rice. If you ever drop your cell phone or other small electronic device into water, place it into a ziplock baggie filled with rice. The rice will absorb the moisture out of the device and hopefully save it from destruction.

Well there you go, 4 different ways to dehydrate something.

Monday, April 2, 2012

Super Mario Bros. on Wii

I mentioned in a previous post that we recently got a Nintendo Wii. We bought the Wii to play the "Just Dance" series of games - not the type of games I would enjoy but I have to admit they are fun, and a workout to boot.

The Wii came bundled with "New Super Mario Bros. Wii," which is a heck of a mouthful. [Why they felt the need to put "New" in the title is beyond me.] At first I wasn't looking forward to this game at all. My reservations about this game were based upon the controller. To play the game you must use the Wii remote held sideways, you cannot use the Wii classic controller. To me the Wii remote is best described as "brick shaped." It does not look like a comfortable controller in the least. As such, I had no interest in playing the game.



As it turns out, I'm glad our console came bundled with this game because otherwise I would have never played it. This game is easily one of the best games of all times. I grew up in the 8-bit and 16-bit video game era. This is a time when the 2-D "platform" genre of games, like Mario and Sonic, were king. I personally feel that Super Mario Bros. 3 on the NES and Super Mario World on the SNES are probably the 2 best games of all time. Well this game takes everything that's awesome about those games and improves upon it. It is a true 2-D platform game, but the entire environment is rendered in 3-D. The graphics and animations are very fluid and sharp. And I have to say, for being a low-resolution system (640 x 480) the graphics are very sharp and clear.

I think one of the things I love about this game is the level of effort and thought that went into its production. One of the great things about this game (as well as SMB3 and SMW) is almost every level has something new, something you don't see on any other level. For example, one level has these giant caterpillars that you can jump on the backs of. No other level has these. So they programmed these caterpillars for one and only one level. I don't know how many levels there are, but it's a lot. So there's a lot of variety.

Now I'm not saying this game is perfect. I still feel like the controller could be better. The Wii remote held sideways is a lot better than I was expecting, but it's far from perfect. Probably my biggest gripe with this game is a feature found in Super Mario World. You could kick turtle shells straight up to knock out flying enemies or open high up question mark boxes. But alas this feature was not added to this game. But it's a small complaint given how great this game is.

In short; if you own a Wii and you don't own this game, you owe it to yourself to buy it. Especially if you are a fan of retro platforming games. If you don't own a Wii, I would argue this one game is good enough to justify buying a Wii! That's how much fun it is!

Saturday, February 25, 2012

Project Genesis - Modding

Now that the repairs and maintenance are complete, I can move onto the final, and most exciting, phase of Project Genesis - "modding." Here I'll modify the Genesis to actually make it better than when it was new. The mods I'm going to make are S-Video, composite, and stereo audio out the back.

Originally the Sega Genesis (Mega Drive) output RF and composite video. My plan is to add a second composite output (more on that later) plus S-Video output which is supposedly much better! This is nothing new, there are tons of people who've performed the S-Video mod and posted their results on the Internet. But all of those posts seem to lack important details. It's my hope to fill in all the missing pieces for anyone else who wants to perform this mod.

To create the S-Video output I need to tap pins 15 and 16 of the CXA1145. Note: not all versions of the Sega Genesis use the CXA1145, so check before you assume this will work. I want to add a second composite output to the back even though the Genesis comes with a composite output. The reason being is the built-in composite requires a proprietary video cable, whereas I'm going to use a standard RCA jack. Finally I plan to add a stereo output jack on the back by tapping the CXA1034P audio chip directly. The schematic for all this should look like this.


S-Video
There are two main S-Video mods I've found on the Internet, David Howland's and Jamma Nation. The Jamma Nation one is newer and uses a much simpler design, so that's the one I'm going to use. However both designs don't give you all the necessary info. I decided to wire up the design on a breadboard first, this allowed me to make changes to the circuit to get the best possible performance out of the mod. Below is a list of the questions about the original designs, and the answers I determined.


  1. Some designs use an amplifier for both the luma and the chroma lines, some designs only use an amp on the luma signal. Is the amp on the chroma line optional? Answer: I tested the circuit both with and without an amp on the chroma - I can say the amp on the chroma line does nothing. There was no noticeable difference in the video output with the amp on the chroma line
  2. Most of the designs call for an NTE85 transistor on the luma amp, but this transistor is no longer in production. What transistor can be used in it's place? Answer: I used a 2n3904 transistor (specifically the 2N3904FS-ND on Digikey) and it worked great.
  3. Many people who've done this mod state that the capacitor on the chroma line must have the positive towards the CXA1145 chip. Others state that the negative must be towards the CXA1145. Well they both can't be right, so which is it? Answer: I tested the cap forwards and backwards, the polarity made no difference. Since the polarity made no difference, I also tested a bi-polar capacitor and that worked as well. Since polarity didn't matter, in the end I went with the bi-polar capacitor.
  4. For the chroma line they say to use a 220uF capacitor, but they don't specify the voltage needed. What voltage capacitor should one use? Answer: Using my multimeter on the live circuit I tested the voltage across the capacitor. The voltage never exceeded 2.5 volts. So for this mod I would suggest 6.3, 10, or 16 volts. Anything higher is just a waste. I went with 10 volt in my design.
  5. Both the chroma and the luma lines call for a resistor, but no one says what wattage resistor to use. Answer: Again I tested this with my multimeter. Both the volts and the current were very low. Even when you multiple them together to get wattage, it was extremely low. So using 1/8 watt resistors leaves enough of a margin. You could also use 1/4 watt if you wanted to.
  6. The luma line calls for a 27 ohm resistor, and the chroma line calls for a 75 ohm resistor. Varying these resistors greatly effects the quality of the video signal. So are these values truly the optimal values? Answer: On my breadboard I replaced the resistors with trimmers so I could adjust the resistance while the circuit was live. This allowed me to test the values to "see" what looked the best. The people who originally came up with this design did a good job, both of their resistor values were very close. The values I came up with as "optimal" were both within 1 ohm of the original values. So in my final circuit I used the 27 and 75 ohms from the original design.


Composite

  1. As with the S-Video mod, some claim the capacitor must have the positive facing the CXA1145 chip and others say the negative. So which is it? Answer: I tested the cap forwards and backwards, as before the polarity made no difference. I also tested a bi-polar capacitor which also worked. In the end I went with the bi-polar capacitor.
  2. They say to use a 220uF capacitor for the compostive, but they don't specify the voltage needed. What voltage capacitor should one use? Answer: Using my multimeter on the live circuit I tested the voltage across the capacitor. The voltage never exceeded 2.2 volts. So for this mod I would suggest 6.3, 10, or 16 volts. I went with 10 volt in my design.
  3. Again, the composite signal uses a resistor, but no one says what wattage resistor to use. Answer: As before the volts and current very very low. So using 1/8 watt resistors leaves enough of a margin. You could also use 1/4 watt if you wanted to.
  4. The original design calls for a 75 ohm resistor, but is this the optimal value? Answer: Testing with a trimmer I felt like the best video came from a resistor of 72 ohms. In my final design I went with the original value of 75 ohms as it's the only value I had on hand.


Stereo Audio

  1. By tapping the CXA1034P directly you bypass the headphone amp and should get a cleaner signal. But is this output too low for the TV, does it need additional amplification? Answer: Surprisingly the audio output directly from the CXA1034P was perfect line-out levels. I did not need any amplification before passing onto the TV.
  2. Does the headphone slider effect the audio output levels in anyway? Answer: The headphone slider does not affect the audio levels in anyway. So tapping the CXA1034P directly is a great way to get clean stereo audio.


Here's the final circuit design I went with:


Here's what it looked like on the breadboard.


Now that I've breadboarded up the design and I'm happy with it, time to solder it all up and squeeze it into the Genesis case. I also need to mount the connectors on the back. Whereas most people drill holes behind the cartridge slot, I decided to use the existing holes on the back of the Genesis. The back of the Genesis had 5 openings; power, proprietary A/V connector, RF output, RF channel selector, and the "EXT" port (which from what I've read was only used for debugging by developers). My plan is to remove the RF unit and EXT port which opens up three holes into which I can mount the S-Video, composite, and audio.

The picture below shows the solder points to clean out in order to remove the RF unit (shown in red) and EXT port (in blue). Getting the RF unit out ended up being tricky. The three point in a row were easy, but the large grounding points are difficult to clear out the solder. I ended up having to use a product call ChipQuik to get the RF unit out. By comparison the EXT port was a snap to remove.


And here's a picture with the RF unit and EXT ports removed.


Now it's just time to solder up all the connections. Here's all the wires connected to the underside of the motherboard.


As you can see in this picture I soldered the capacitors and transistor inline, covering the connections with heat shrink.


And the completed view of the back of the Genesis. I ended up drilling a single hole for the audio jack. The EXT port ended up being too large for the connectors, so that opening went unused.



And finally, the results...

Here are pictures of the composite versus S-Video output. As you can see the S-Video is noticeable clearer. You'll also notice the composite pictures are darker. There wasn't a change in the lighting, nor post image-processing, the S-Video output is just brighter and more vivid.












This mod was a lot of fun, and the quality of the video output was more than I had expected. If you're into retro gaming on the Genesis and if you have soldering skills, you might want to consider this mod. In the end the Genesis cost me $1, and the parts cost about $10.

Monday, February 20, 2012

Project Genesis - Maintenance

Now that the Sega Genesis has been repaired, I can move onto the maintenance phase. Whereas the console does work, it's 22 years old. So I want to do some preventative maintenance with the hopes it will work for another 22 years.

The first step was to clean the Genesis real good. For this I completely disassembled it. I washed the plastic parts in warm soapy water. The circuit board got blown off with a few blasts from my air compressor. And the connectors on the back of the unit got cleaned using a cotton swab and rubbing alcohol.

Probably the biggest problem with these old consoles is a bad connection between the cartridge and the console. If the console doesn't work, chances are the contacts are dirty. Previously I discussed how the best way to clean these connectors is to use cotton swabs with vinegar and rubbing alcohol (separate passes - not mixed together). This is easy to do for cartridges, but doesn't work for the console as cotton swabs don't fit into the cartridge slot. What I find works the best is to take a thin ridged object like a credit card, cut a strip of paper towel to the thickness of the credit card, fold the paper towel over the credit card, and use this go in and out of the cartridge slot. First use some vinegar to remove the oxides on the metal. Then change the paper towel and do a second pass with rubbing alcohol. This should do a great job of cleaning the slot.


The next step is to ensure there are no cracked solder joints. The repeated stresses on the board over the years might crack solder joints. If the console has intermittent problems, if one or both controller ports don't work, etc. then it's very likely there are cracked solder joints. Even if you don't have problems, it can be a good idea to touch up the joints. To do this you'll need a soldering iron and a little bit of leaded solder (I don't recommend lead-free solder as it tends to crack over time and is harder to work with). The best is a temperature controlled soldering station, but you can get by with an el-cheapo Radio Shack iron. A smaller tip is very helpful. Lastly, buy smaller diameter solder. To touch up the solder joints you do the following for each point:

Place the hot iron on the joint to melt the existing solder. Technically that's all you need to do. But it can be a good idea to add just a little bit of new solder to each point to ensure the joint is even stronger. You don't need to do this for every solder point in the console. Pick the joints that are most likely to have physical stresses. Those include the controller ports on the front, the audio/video and power connectors on the back, as well as the row of pins for the cartridge connector.


There was one more step which I considered but ultimately decided against. I was going to replace the electrolytic capacitors (a.k.a. recap the Genesis), but in the end I decided against this. Capacitors are often times the first components on a circuit board to go bad, so replacing them can be a good idea. I decided against this for several reasons. First, using what's called an ESR meter I tested the existing capacitors and they tested good. And the second main reason is because it's a lot of work. This model Sega Genesis has over 30 capacitors which is a lot. The cost is cheap, the replacement caps cost about 40 cents each.

Check back next time for the final task - modding.