Turing-Welchman Bombe physical build – part 2.

July 30th, 2015

Even though I keep procrastinating about working on the Austin 7 it actually works out well since I am motivated to work on the Bombe. Following last nights few hours in the shed making the little clips, tonight I did a few more hours and started on the hubs. I didn’t complete them, it started getting late and I figured my chances of muffing something up were increasing so I came in, but I made good progress.

I started with a small bit of aluminium bar that happened to be the right diameter. This was lucky since it was the only bit of aluminium bar I had! I faced the ends in the lathe then drilled an 8mm hole through the middle for the main shaft.

IMG_4037_1 IMG_4039_1

I then glued a paper cut out of the hub to the end. While the glue dried I changed the 3 jaw for the 4 jaw chuck in the lathe. To get everything the same I wanted to drill all the holes at once in one piece of aluminium them slice that up to make the three hubs. With a 3 jaw chuck anything round you put in it will automatically be centralised as the three jaws all move in and out together. In 4 jaw chuck however the jaws all move independently which means you don’t have to have things centralised as all. So I was able to have the bar off centre and drill each hole that way.

IMG_4040_1 IMG_4041_1

Being off centre does mean you can’t run the lathe too fast. Off centre means it is also out of balance and if you go too fast everything vibrates. It took quite a while to drill the holes as they needed to be deep enough that I could do all three hubs at once. It meant much drilling then backing out to clear the bit of swarf. Including the centre hole there are ten to drill in total. The four holes for the mounting bolts are plain but the others needs to be tapped so they are drilled undersized.

Here I wasn’t quite sure how best to proceed. And I forgot to take pictures! The question is do you cut the three hubs then knurl each one or do you knurl the thing in one piece then slice it (knurling being the rough, diamond pattern on the edges of the hubs). I did the latter. I first used a parting off bit to cut grooves in the bar. I couldn’t part all the way through anyway because of the holes through it. I find when I try that the tool catches in the holes and makes a mess of things. So I just made a groove as a guide to cut the slices with a hacksaw by hand.

I did that then had to think how could I face the front and back of the hubs without crushing my knurls in the jaws of the chuck (back to the 3 jaw now of course). In the end I simply wrapped the hubs in layers of electrical tape. I was then able to grip them in the chuck without damaging them. I was then able to clean up the faces. The advantage of doing it this way is it makes the faces nice and flat. If I faced them then knurled the knurling would leave a slightly rough edge on the faces. Knurling doesn’t cut the metal, it deforms it.  I also used the parting off tool to remove the knurling from the bottom of hubs as on the originals.

IMG_4042_1

Next I needed to countersink the holes for the four mounting machine screws. These go through the hub, through the Tufnol face plate, through the tin and through a plastic disc that will be inside the tin to allow me to mount it all to the motor shaft. To countersink the holes I used my pillar drill. The depth stop on it is broken but with a moveable table that doesn’t matter. I simply put the bit in the chuck then set the table height so that when the drill is all the way down it is where I need it. I set it a little short then to make adjustments I use spacers (blocks of wood, sheets of metal and so on) under the workpiece as I drill. That way you can get the depth consistent for all the holes.

IMG_4049_1

To get the heads of the mounting screws just under the surface I set up the drill so they were flush then used a 0.5m sheet of brass under the hub to lift it up and increase the depth of the hole 0.5mm. The heads much be under the surface so the mounting clips don’t hit them.

I still need to tap the other holes. There are the holes for the screws that hold the little clips in place. One for a larger screw that holds the end of the clips down.  And two for little stops that stop the clips opening too far. The recreated Bombe at Bletchley Park seems to use little screws here on some drums, the heads acting as stops. But John Harper’s rebuild page shows little pegs being used which is what the US Army diagram seems to show.

I’ll probably use pegs. These little details are so tricky to get right and probably no one but me notices or even cares but it’s nice to do thing properly as far as you can. I wish I hadn’t lost the film of photos I took on my last trip to Bletchley as I know there were some close up shots of drums on that.

The hubs will look something like this once on the drums.

IMG_4050_1 IMG_4051_1

The other thing I did today was try to find some Letraset dry transfers that I can use to do the lettering on the edge of the discs. Seems this stuff is now hard to find (in NZ at least). I found some places online selling something but their web sites are crap and they don’t specify what exactly they are. I need 1/8th (3mm) white letters. I will try Gordon Harris tomorrow and I think maybe Spotlight has something as well?

I am so glad I only have to make three of these damn things. Makes you appreciate the work that went into creating hundreds for the rebuilt Bombe at BP. One interesting thing to note is the three indicator drums are slightly different to the main drums on the Bombe. For a start the letters go around in the opposite direction. Also the main drums and faceplates have holes in them. The three indicator drums have no holes. I am guessing it’s something to do with the indicators not having any brushes?


Turing-Welchman Bombe physical build.

July 29th, 2015

I think last update I had the Turing-Welchman Bombe working on breadboards. I have now made the electronics neat and tidy with a circuit board for the electronics and a base board to mount all the various modules to.  And I have started the rest of the mechanical build.

The whole time I am doing this I can’t help thinking of Harold ‘Doc’ Keen the engineer who actually did the physical building of the Bombe from Turings design (no, unlike that film Turing didn’t single handedly build it himself).  If you think Welchman is forgotten poor old Harold is even less well known.

The base board is made from the same stuff that I am making the drum faces and the front panel of the Bombe from. Now I don’t know what the original used but it looks to be some kind of phenolic board. I bought some of this from RS locally which seems to be similar. I got a dirt cheap Arduino clone and a prototyping shield to which I added connectors to join to the rest of the circuit.

I made a base board and mounted the Raspberry Pi 2, Arduino, three driver boards and my interface board to it. The interface board is just made from some Vero board. It contains the 4066 switches that control the signals going to the second and third drums and the transistor buffers that let me safely interface the 3.3 volt Pi to the 5 volt Arduino. It also has the voltage regulators (5 volts for the computers and 12 for the stepper motors) and heat sink for the power supply.

IMG_4007_1 IMG_4009_1 IMG_4008_1

The boards are all mounted with 3mm screws and small nylon spacers. They also came from RS. Jaycar (who are just down the road) sell spacers but at some ridiculous cost for what’s basically a little plastic tube. The interface board connects everything together. The 20 character, 2 line display I ordered also arrived so I wired that up too.

I made all the interconnecting wires, except those carrying power, red as the original Bombe seems to have a lot of red wiring internally. To hold the loom together I tried cable lacing. This is when you use a waxed twine to tie the cables together into a loom. Now, this being NZ – the land that time forgot, you can’t actually get lacing twine here. In the end I bought some waxed cotton cord from a bead and jewelry place. That seems to work very well.

IMG_4010_1 IMG_4012_1

IMG_4017_1_1 IMG_4014_1

It’s not the neatest cable lacing in the world and I was considering redoing it all then I found some pictures online of the rebuilt Bombe at Bletchley and see it’s not perfect either so mine is in keeping with the original!

With everything mounted I was able to fire it all up and check it works. I have set up the Pi so that when it boots it automatically runs the Bombe software. That reads from a USB stick which contains the menu to run. If the menu is correct the Bombe will then start running when the start button is pressed. The start button is one of two buttons that will be on the front of the Bombe like on the original. The original Bombe has two buttons on the front but also a physical lever on the side next to the indicator unit that needs to be moved after the Bombe stops and you wish to restart it. I have simplified my interface to just a start and a stop button. Pressing the stop button will pause the Bombe. Holding it down for more than 1.5 seconds will abort the Bombe run and drop out to the command prompt. Normally however the Bombe will complete it’s run and automatically shut down the Pi once the stop button is pressed a final time. The Pi doesn’t have an on/off switch so the nice way to shut it down is with a command from the command prompt. I have a simple script that starts up the Bombe code then checks the return value when it exits to see if it should shut down or simply exit to the command line.

I did get a WiFi dongle working but since the Bombe doesn’t normally need an Internet connection to run I haven’t used that in the model. If you need to connect to it you can connect an Ethernet cable.

With the electronics done, mounted and tested I started on the rest of the model. I rough cut then machined three discs of the Tufnol material to go on the front of the drums.

IMG_4002_1 IMG_4003_1

The material machines really easily. It’s easier to bolt the three together and machine them all at once so then they are exactly the same diameter.

I also drilled an 8mm hole in the base of the tins I am using for the drums. I made a rough wooden disc to sit in the tin so when it was mounted on the lathe the bottom of the tin was hard up against the wood so I could center drill then properly drill it.

IMG_4004_1 IMG_4005_1

Tonight I made the metal clips that go on the front of each drum to hold it to the drive shafts. Originally these would have been stamped out I guess. I only need six of them for my three drums so to make them I cut out six pieces of 1mm steel sheet and soft soldered them together into one solid lump. I then printed out a picture of the drum from the US Army Bombe report shrunk down to my 3/4 scale. I cut out the shape of the lever and glued that to the steel block.

IMG_4018_1 IMG_4019_1 IMG_4020_1

I then drilled the holes right through the block and started shaping it. Rough shaping was done with a hack saw and a bench grinder. Then I progressed to hand filing and finally a Dremel with various grinding attachments.

IMG_4021_1 IMG_4024_1

IMG_4025_1 IMG_4027_1

After much shaping I ended up with a single block in the right shape. I then simply unsoldered the pieces and wiped the solder off with a cloth. Each piece was then rubbed on some sandpaper to make them smooth.

IMG_4028_1 IMG_4030_1

They are not perfect but are bloody close. I still need to make the spring mounting posts, which will be brass and soldered in place, then I will zinc plate the clips.

IMG_4033_1 IMG_4034_1 IMG_4036_1

I have tried to find screws (mostly from RS again) that match the appearance of the originals and luckily enough so far all the screws I have found have heads that are almost exactly the right size and shape.

Next I need to make the hubs from aluminium which will need a bit of careful machining and drilling. I ordered (again from RS) some little castors and soon I will start making the actual frame once I get my acetylene bottle back off a friend who borrowed it. I also need to machine up the little finger posts mounted to the front of the drum used to help the operators turn them to the right position.


Turing-Welchman Bombe update.

June 24th, 2015

A quick update on my Bombe project. I am too lazy to write it all up in detail so here is a film!

I had sent links to the previous update to the various geek sites but no one was interested I guess. Not geeky enough? Even with a Raspberry Pi, an Arduino AND a homemade 6502 computer? Perhaps I need to mention 3D printing more although I am going to be construction my Bombe from good, honest steel!

Anyway, for those who are interested (hopefully you’re both sitting comfortably!) I am making good progress. I have the code running on the Pi now. Porting it from my laptop was simple as I had deliberately made the code as portable as possible. It’s in very simple C++. Interestingly, running flat out, a run of the BP weather report menu takes 11 seconds on my laptop. On the Pi2 it only takes 22 seconds. I am impressed by that! If I can ever get it running again I would like to try it on the Original Pi model B to compare the speed.

Never having used a Pi before (well, as anything but a media player!) it took a little bit of fiddling but in the end I got the GPIO pins working using the WiringPi library. This is very simple to use. I can see why the Pi is taking off as an educational computer. It’s really very powerful and quite easy to use and there is a ton of information and tutorials about. Trying to get wireless working on Linux, well, that’s another story so I am on a cable for now.

I have the Pi connected to the Arduino directly. You have to be a bit careful here. The Pi works on 3.3 volt logic while the Arduino is using 5 volts. Since I am only connecting to inputs on the Arduino I can actually get away with feeding the Pi outputs directly into the Arduino inputs. This is a bit dangerous. If you get it wrong and the Arduino feeds 5 volts into the Pi bad things could happen! In my real version I will use some kind of buffering/level shifting between them.

The Pi controls 2 lines on the Arduino. One tells the Arduino to increment the fast drum one letter. The second tells it to stop. The stop signal is to simulate the real Bombe stops where the fast drum actually rotates past the stop then has to come back around into position. One thing I had to do was tune the speed of the steppers with the step pulse from the Pi. In order for the motion of the drum to be as smooth as possible you want each movement of the stepper to take almost the same amount of time as the time between the pulses. If the motor runs too fast then it will move then stop until the next step. That makes it’s motion very jerky. With the time of the step being just fractionally shorter than the time between pulses that stop time is minimised and the motor motion is much smoother.

I also decided to use an LCD display on my Bombe as the test register indicator (which is on the right hand side of the real Bombe). You can see a picture of the indicator unit on this page: http://www.rutherfordjournal.org/article030108.html That one is mechanical, a bank of relays popping up a pin or a needle to indicate the letters.

I ordered a 40 by 2 line LCD display off eBay so I can display the letters of the alphabet on one line with the indicators below. For now I am using a 20 by 4 line display for testing as that’s what I had in the junk box. The wiringPi library has an LCD library built in so that was also very simple to get working.

I need to do a little more playing then I can make a nice board for the circuit. It’s fairly simple so I will just use Vero board. I might use an Arduino Pro mini as I think I got extra when I build my Enigma machine wrist watch. One thing I do need to work out is resetting the Arduino at the start of  a run. Currently I just do this manually but I will control that from the Pi also. I will also need to simulate the lever on the side of the Bombe that is part of the start/stop procedure.

The trickiest thing will be how to actually choose the menu to run. The Pi will just be used in the desktop Bombe machine. So I need to work out how to boot it up and only run that. But then I need a way to load in the menu file. I think the way I will do it is to have the menu on an SD card and the machine just reads it off there. I think one way to add a second SD card slot is with one of those USB SD card readers. I have a feeling that will all just work. The other option is using the SPI interface on the Pi to read a card directly but I think the USB option will be much easier! Will need to do some experimenting there.

The other thing I must decide is do I want a bank of 26 switches down the side to select the input letter voltage. I would need some circuitry to work out which switch (or switches, theoretically my code should handle multiple inputs!) is currently on. It seems odd to have all the menu setup on an SD card except for that one input voltage setting. So I could just have the switches and not wire them into anything! But then why have them apart from for looks? I’ll decide when I start building the housing. I think if there is room I will have them!


A Turing/Welchman Bombe.

June 14th, 2015

This is the next step after my Enigma machine wristwatch project and putting my Orwell computer into a case. The next step was to investigate and hopefully simulate the Turing/Welchman Bombe. The Bombe is the machine used by the British in WW2 to help decode Enigma encoded messages. If you saw the film The Imitation Game you probably have some idea what the Bombe is.

IMG_3636 Enigma machine wristwatch.

IMG_3783_1 Orwell 6502 computer.

Unfortunately your idea will be mostly wrong! Turing didn’t build the Bombe single handedly. He didn’t have to fight anyone to build it. It wasn’t called Christopher and it didn’t just spit out the solution ready to be entered into an Enigma machine to decode messages. Seems Hollywood can’t make a historical film without ‘tweaking’ it for the sake of story. They even made the actual physical machine different to reality, making it bigger and with more red cabling to make it look ‘alive’. The way they are shown operating it is all wrong too. And the drums don’t move correctly. Bah!

If you want the short(ish) version here is a film of what I am doing:

If you want to skip to seeing the Bombe code written in BASIC the go here: http://www.asciimation.co.nz/bb/2015/06/14/turing-welchma…mbe-basic-code

If you want the long version keep reading! I did comment once I didn’t think anyone reads all of this stuff I put up and got comments saying otherwise. We’ll see how that goes after this post. It’s kind of long!

Many thanks to John Harper, who lead the real Bombe rebuild team, for answering my questions about how a real Bombe worked. What I have done is simple compared to what John and his team did in reconstructing the real thing. That took years of hard work and research and it really is well worth going to Bletchley Park to see it in action (I am definitely going again next time I am over there).

Also thanks to Bletchley Park themselves who saw my Enigma watch project and when I explained I was also making a Turing Welchman Bombe they found a copy of Bletchley Park report 4 to send me. I actually joined the Friends of Bletchley Park. Obviously the free entry and events aren’t much use to me (me being in New Zealand) but I want to support what they do there and that is a good way to do it.

Also thanks to Magnus Ekhall who is one of the creators of the brilliant online Bombe simulator here. If you want to play with a Bombe yourself don’t bother with my version, go and try that one! That site has a good tutorial you can follow to wire up and run the Bombe in the same way they run the real one at BP. I used their version to double check that mine was giving the same results.

I should mention here, because if I don’t I will probably get comments about it, that yes, the Polish also had an Enigma cracking machine. That was called a Bomba. The Poles did indeed crack the Enigma code very early on. The machine the Poles made exploited a procedural flaw in the German method of enciphering. It similarly used the idea of multiple Enigma machines connected together (6 in their case). But other than that I don’t think the machines are in any way connected (other than by name maybe) as they work on totally different principles.

I wanted to find out what the real story was and how the Turing-Welchman Bombe really works. Wikipedia gives all the basic details as do many other sites about. And there is of course the reconstructed Bombe at Bletchley Park in the UK which is well worth seeing.

The best places to read about the Bombe are as follows:

https://en.wikipedia.org/wiki/Bombe

http://www.ellsbury.com/enigmabombe.htm

http://www.codesandciphers.org.uk/virtualbp/tbombe/tbombe.htm

http://www.rutherfordjournal.org/article030108.html

That last link is a cut down version of Bletchley Park report 4 available here.

Other useful books and documents are Demystifying the Bombe by Dermot Turing (Alan Turing’s nephew).

Gordon Welchman’s Hut Six Story has some good details, especially on the diagonal board.

And the US Army 6812th Division Report from 1944 available on the late Tony Sale’s site.

The other site that will be extremely useful for the physical part of the build is John Harper’s Bombe rebuild site. John has been very helpful answering some of my questions and letting me know some of the details of the real Bombe and how it works. The site is a little out of date now he tell’s me but the information there is brilliant.

And of course the best thing you can do is go to Bletchley Park itself and see the reconstructed Bombe running!

I set about figuring out how the Bombe works myself so I could build my own version in software. I’ll give how I worked out in my head how the Bombe works. This probably isn’t a good way to explain it, check out the links I provide for that, but this is how I think of it. In very simple terms what it is is a giant circuit tester. To know how it works you need to know how Enigma works of course.

Enigma can be thought of as a machine that changes an input letter into a different output letter. A letter can’t, due to the physical nature of the machine, become itself. The letters are changed by sending a current through three rotors, each of which changes the letter (or not!), then through a reflector (which always changes a letter hence no letter coming out as itself) and back through the three rotors (changing the letter each time again). There is also the plug board which swaps the input and output letter to some other letter (or not). We can ignore the plug board for now. After each letter is encoded the position of the rotors change (in a known pattern) so for the next letter encoded the current will take a different path though the machine. This is why if you hit the same key on an Enigma repeatedly a different letter is likely to come out each time.

To make the Bombe work we need what was called a crib. A crib is a guess of  what the unencoded text of an encoded message might be. For example if you have an encoded message and you know the last words in it are always ‘Heil Hitler’ that is you crib, you now have the encoded letters and what they decode too. For example the first H might appears as a ‘U’ in the encoded message. Because of the way Enigma and H entered will return a U and a U entered would return an H, for that given position of the machine.

That last bit is important. It works only in that position. The crib gives us two bits of information, what letter transformation took place and at what position did that happen.

The crib they use at Bletchley Park to demonstrate their real Bombe is the weather forecast crib shown below. Wetter vorhersage means weather forecast in German.

Position:      1  2  3  4  5  6  7  8  9  10 11 12 13 14 15 16
Cipher text:   S  N  M  K  G  G  S  T  Z  Z  U  G  A  R  L  V
Plain text:    W  E  T  T  E  R  V  O  R  H  E  R  S  A  G  E

From the crib we can see at position 1 ‘W’ was encoded to ‘S’, at position 2 ‘E’ encoded to ‘N’ and so on.

With an Engima machine correctly configured typing in each letter of the plain text in sequence will give you the matching letter of cipher text. Also if you were to type in the cipher text you’ll get back the plain text. That’s how who ever receives the message can decode it as long as they use the same settings. The Bombe helps find the settings (it doesn’t crack teh code itself like the films have you believe).

Instead of using one Enigma machine imagine you had 16 of them, all with the same configuration i.e. same rotors, same rotor order, same reflector, etc. You type ‘W’ on the first one and get out ‘S’. If you now go to the second machine and type in ‘E’ the rotors would need to be moved on 1 position from the first machine in order to get out ‘N’. In a real machine that position change happens automatically when you press a key. If we have 16 Enigmas we’d have to set the positions ourselves manually, the second machine at position 2, the third at position 3 and so on.

When doing this we are ignoring things like the rotor turnovers. We’ll get to that in a bit.

Now if you don’t know what the rotors are or what the starting position is one way to work it is to try them all! So you set up your 16 Enigmas with the same three drums in the same order in each one. You set up the first machine with the wheels at ZZZ, the second one to ZZA, the third to ZZB and so on. Then you could see if the encodings at each position come out as you would expect. If a letter in the sequence comes out wrong you know that can’t be the right setting so you would try another, maintaining the same relative position for each of the machines.

Say you decided to try RST on the first machine. To maintain the correct relative positions you would set the second to RSU the third to RSV and so on. By doing that you could eventually run through all the positions until you found one where the sequence came out correctly. Since each wheel has 26 letters that means 26*26*26 or 17576 possible combinations.

The Bombe basically goes through all these combinations but it is using some cleverness to do the ‘checking’. Remember the Enigma work on electrical circuits. When a key is pressed a current is sent through from that letter and comes out another letter.

If we have out 16 Enigmas set up and could arrange it so that the current was sent from one machine to another instead of manually pressing keys we could trace where the current goes and make sure it comes out in the correct place.

If you look at the crib again you can see in the correct position a ‘W’ on the 1st machine should come out as an ‘S’. Looking along the crib we can see that at position 7 an ‘S’ turns into a ‘V’. If we wired up the machine so that the ‘S’ on the 1st machine went into the ‘S’ on the 7th machine that should cause the output to be a ‘V’. We could then also wire up the ‘V’ on the 7th machine to the ‘V’ on the 16th machine and get out an ‘E’. The ‘E’ could wire to the 2nd machine and output an ‘N’. The current is going though four different Enigma’s automatically rather than us checking each individually.

Note that is doesn’t matter if the letters we use were in the crib or in the cipher text. It doesn’t matter since the transformation works the same both ways.

For understanding the Bombe we can reduce the Enigma machine into what’s called a scrambler. A scrambler is an Enigma machine with a given rotor choice and order.  For now we ignore the plug board and we can also ignore the ring setting. A scrambler has 26 inputs and 26 outputs corresponding to the letters of the alphabet. At a given time, one letter goes into a scrambler and a different letter comes out. In Enigma the current goes into the machine via a ring of 26 contacts (one for each letter) then comes back out on the same ring in a different position. Our scramblers however are double ended. You have one ring of 26 contacts and a current on that ring will cause a current to appear on another ring of 26 contacts. This works in both directions so if ‘A’ on the first ring goes to ‘G’ on the second ring an ‘A’ on the second will go to an ‘G’ on the first. It doesn’t matter which way you send current through the scrambler, the transformation is the same.

These double ended scramblers let us easily connect up the letters in the menu as we did above and send currents though them. If you just send the current through one scrambler there is quite a high chance that the correct output letter is seen even if you have the wrong settings on the scrambler – a false positive. If you send it through one scrambler then send the output of that through another as we did above and the right letter comes up we’ve reduced our chance of a false positive because now both scramblers have to be in the right positions. In our example above we sent it through four scramblers which all have to be in the right position.

You can draw out what we did above like this:

W – S – V – E

The lines – represent the scramblers. We can write in what each scramblers relative position is.

W -(1) – S – (7) – V – (16) – N

We can go ahead and make other connections based on the crib we have. If we draw all these out we end up with a diagram like this:

IMG_3878

Since each scrambler, the lines on the above picture, is an Enigma machine we can show the positions as the positions of the three rotors in each scrambler. We assume a pre-starting position of ZZZ for the rotors so position one is then ZZA. Position 2 is ZZB and so on. In an Enigma machine the rotors move BEFORE the letter is encoded. So if the rotors are in position ZZZ the first actual encoding is done in position ZZA.

Redrawing we get this what is called a menu.

IMG_3877

What we now have is a diagram showing us all the transitions we know we should get from our crib and in what positions they happen and how they are linked together.

This is where things start getting complicated.

One thing you can see on the menu is we have a closed loop: G-E-V-S-A-R-G. If all the scramblers between all those letters are in the correct positions then we should have a closed loop. If you put a current in at G it should go around the loop of scramblers and only come out on G.

This seems simple enough but for one thing. Remember we ignored the plug board earlier. Now it is back to complicate matters. The menu shows us the transformations as if there were no plugboard. But the plug board was always used when encoding Enigma messages. It swapped 10 pairs of letters with another letter. So in the menu above the ‘G’ we see isn’t actually what we want to use as the input into the scramblers. This is because the ‘G’ in the message might have been through the plug board in which case the letter fed into the scrambler may be something totally different.

This is one of the clever bits Turing worked out. We make an assumption about what letter we think the ‘G’ in the message was connected to on the plug board. We feed that letter into the scramblers. So if we assumed ‘G’ was plugged to ‘k’ we would put a current on the ‘k’ input of those scramblers connected to ‘G’ in the menu. And because ‘G’ goes to ‘k’ then ‘k’ also goes back to ‘G’. We write the assumed partner lower case to avoid confusion with the menu letters.

Because we have a loop if feeding the voltage into ‘k’ isn’t correct it will come out on another letter. Turing’s idea was that the letter that did come back out  is then used as a new guess to what the partnering is. This happens automatically because of the way the machine is wired up. Remember the current flows both directions though the scramblers. I didn’t when I did my first algorithm but more on that later!  There are various possible outcomes when we do this.

You’ll note that those conditions above aren’t mutually exclusive. In the third case when the scramblers are in the wrong position we could end with similar result to the first two cases!

Now you might be able to see why the menu is important. The more loops you have the more chance there is of the current working it’s way around the circuit and eliminating the false positives. But you don’t want to have too many letters in the menu because something else we ignored will then come back to bite you.

In a real Enigma machine the rotors are arranged to turn over at certain points. In our menu we are assuming that this isn’t happening so that our relative positions are correct. If a turn over had occurred in the message during the crib text the relative positions of the rotors is now completely different. If you have a crib longer than 26 letters you know that a rotor turn over must have happened and so your relative positions will be all wrong after a certain point. If you have less that 13 letters in the crib you’re reduced the chance of that happening by half. You can’t have menus that are too short though because then you’re not providing enough paths for the currents to work their way around the machine.

The physical Bombe is wired up to do all this testing above. When you look at a picture of the Bombe you will see rows of drums. The drums are split into three banks, each has 12 columns and three rows. Each bank can be though of a separate machine electrically (they can be wired together though).  Typically a menu is wired up using one bank.

In a bank the columns are the rotors of our scramblers. So each column is one scrambler. The drums are versions of the Enigma machine rotors wired to be double ended. Each drum shows around it’s edge the letters of the alphabet, A-Z. The drums rotate clockwise and the letters are arranged to go in sequence (so reading the front of a drum the letters go backwards). The colour of the drums correspond to the number of the rotor. This is why when you see pictures of the Bombe (in colour!) the colour will be consistent across the rows of each bank. Remember each scrambler in a menu has the same rotors, just the relative positions of each rotor are different.

The machine also has three indicator drums on the front. These spin in synchronisation with the other drums and are important for telling us what the ‘stop’ is. That is the settings the machine is showing when it finds a possibly correct position for the drums. The indicator drums also spin clockwise but the letters are arranged in reverse order. This becomes important later.

The back of the machine allows wiring up of the menu. The scramblers (each column) are wired to other scramblers as shown on the menu. The reflectors are housed on the left hand side of the machine and each scrambler connects to them. The right side of the machine has switches to allow you to select the input test letter (‘k’ in our example above). Then there is what’s called a test register. This is connected to one of the letters in the menu (the connections between the scramblers). The test register shows the current on every input/output on the connected scramblers. There are other controls we don’t need to go into now.

The menu is wired up on the machine and the drums of the scramblers are set to their starting positions also shown on the menu. When the machine is turned on the test letter current is sent into the menu into the test register menu letter. From there it flows around the circuit to all points it can reach. The most likely situation is that the positions of the drums are wrong and so the current flows around and around and eventually (well, almost immediately since it moves at the speed of electricity!)  it gets to everywhere. Most likely it will get to all 26 terminals. In this case the machine steps ALL the scramblers to the next position. All of the have to move since we need to maintain the relative positioning. It then checks the next position.

If the current didn’t get to all 26 terminals then we might possibly have one of the conditions mentioned above so the machine stops. A stop doesn’t guarantee the position is correct however. Each stop had to be checked to find the wrong ones. That’s a whole other machine and procedure I haven’t even got to yet! A stop that doesn’t correspond to a correct position is called a false stop.

One interesting thing to note here is that while in an Enigma machine it’s the right most wheel that moves (so it it was at ZZZ it will go to ZZA) in the Bombe it is the top drum that moves (so it goes from ZZA to AZZ). Because we’re doing all the positions though this doesn’t matter! This is also why we can ignore (for now) the ring settings. In the Enigma the ring setting is just an offset of the internal wiring of the rotor from what letter it says it is on. Again because we are testing all positions we don’t need to worry about this yet.

The ring setting does however come into it later on when they use what the Bombe tells us to actually try to break the code. And it is also because of the ring setting that the indicator is lettered backwards.

The description above is of the Bombe as Turing designed it. You can see that the only letters involved are the ones actually in the menu since the current can only move between each letter via the connection through the scramblers. It relied very heavily on the menu containing loops so that current sent on the wrong path would flow back around to go though the loop again. With no loops the current would flow from one end of the menu to the other and not be able to feed back in. The test register would have many positions without current on them so the machine would stop. These are most likely false stops.

This is where Gordon Welchman comes into it. What he did was very clever and it’s a complete travesty he wasn’t even mentioned in that Imitation game film! This took me a long time to get my head around but apparently it took Alan Turing a minute or two to get it so I don’t feel too bad (despite it taking me months)!

Welchman realised that the plug board complexity actually helps us out but providing many more paths for the current to flow around the machine. The reasoning is this. We make an assumption that one letter is steckered to another, ‘G’ is steckered to ‘k’. Because the plug board swaps pairs of letters that mean that ‘K’ is also steckered to ‘g’. So in our machine is we send the current in on ‘k’ to menu letter ‘G’ and it comes back out on ‘r’ say we can now say ah-ha, because ‘r’ came out on ‘G’ then that means a current could also go from ‘R’ to ‘g’. So we create a new current and bung it back through the circuit. The way we create that extra current is with the diagonal board.

The diagonal board is literally that. There are 26 rows corresponding to the 26 letters of the alphabet that could be in our menu. Each row has 26 columns also corresponding to the alphabet letters. The rows and columns are wired together so that Row A, Column B goes to row B column A. Row A, Column C goes to Row C column A and so on. If you start drawing it out you see where the name comes from, the connections are physically diagonal!

IMG_3879

Basically the diagonal board vastly increases the number of currents flowing about in the machine and helps eliminate a lot of the false stops. It was a massive improvement on the origin Turing machine hence the machine becoming known as the Turing-Welchman Bombe. One thing to note about the diagonal board is that only the diagonals that are themselves involved in the menu help in this process. This is much easier to see in a software version than on the real machine.

If you’ve got this far this is a good time to point out another massive flaw in The Imitation Game film. In that Sherlock builds the machine and THEN figures out about the cribs from a girl who in the bar at Bletchley Park (that bar set really is in Bletchley Park – the rest isn’t) about the cribs. That’s all totally backwards. It’s like watching a film about Apollo 11 where they built the Saturn V and Apollo spacecraft and then decide to go to the moon. Also I doubt a girl at Bletchley would be talking about EXACTLY what her job there is. They took secrets a lot more seriously back then.

Right, onto my version of the Bombe. I have written up the BASIC version in another post here: http://www.asciimation.co.nz/bb/2015/06/14/turing-welchma…mbe-basic-code

All the code is available there. That version is a simplified version of the Bombe set up to run the Bletchley Park weather forecast menu they use to demonstrate with there. The code can be easily modified to run other menus. A full Bombe run on Orwell running at 1Mhz takes around 4.8 months!

My C version is a port of that code with minor differences. It is much more configurable, the menus and settings are read from a file. I haven’t quite finished it yet but the code is running and on my Intel i5 2.2Ghz machine it takes about 10 seconds for a full run.

It will also simulate the actual motion of the real Bombe but turning the fast drum 1 and a half revolutions for each middle drum turnover. It will drive my hardware correctly then. I have it running on my laptop in Visual Studio so the next step is to get it running on the Raspberry Pi 2. Once I have done that I can interface it to the Arduino driving the stepper motors. Once all that is working I will build the physical model. I am glad I only have three drums to build and not the hundreds they needed on the real thing!

IMG_3870 IMG_3872

IMG_3875 IMG_3876

Now what I have described here is only part of the whole Turing Welchman Bombe Enigma solving process. There is a lot more we haven’t even started talking about. My version of the Bombe is what is known as a Spider Bombe. There is also what was called the Jumbo Bombes which did extra checks to eliminate false stops. I have actually written the Jumbo logic into my C version but since the Bletchley Park reconstructed Bombe is a Spider one I won’t use those in my little model (or will at least make it an option that’s off by default).

I haven’t said anything about how they checked the stops were accurate and how they then used the correct stop to crack the actual Enigma code. That is a whole other complicated story in itself. Again I just want my model to do the same thing as the BP machine. I don’t actually have any pressing Enigma coded messages I desperately need to decode right now!


Turing Welchman Bombe BASIC code.

June 14th, 2015

This is a follow on from my software Bombe post here: http://www.asciimation.co.nz/bb/2015/06/14/a-turingwelchman-bombe

The code is written to run on my home made Orwell 6502 computer. Orwell runs a modified version of MS Basic which is the same BASIC run on many 80s computers so this code should be easily portable to run on any similar machines. The code can be run on an Apple 2 (or the online Applesoft emulator here) with one simple change. More modern BASIC version will probably need more tweaks as MS BASIC has some quirks (like dimensioning arrays!).

IMG_3881_1 IMG_3882_1

All the code is below, just copy and paste it. I will describe the general algorithm. Anyone really interested can work the rest out from there. My software Bombe has a few limitations, well simplifications. The BASIC code is written to run the Bletchley Park weather forecast crib and menu. It can be modified by changing the code directly but you need a good understanding of what menus are and how they work. It does not simulate the entire mechanical Bombe which has three banks of drums, called chains, that can be hooked together in various ways. My Bombe is really just one chain but that chain can have many scramblers (although more than 13 or so would be unusual). The BP example menu uses 13.

Given that BASIC is so slow you can configure the code to start offset from ZZZ. This is so you can see it working quickly. Remember a Bombe goes from AZZ to ZZZ in a complete cycle. Running on Orwell as I write this it has been running now for 68 hours and 45 minutes and it’s only up to YMZ!

I don’t guarantee that this code is bug free! But it should run the Bletchley Park menu and give the correct two stops. These are DXK:Q and FAN:K. The first three letters are what the indicator drums will show and the second is what the test register should show.

The general idea is as follows. Everything works around the diagonal board, the 26 by 26 array of letters. The rows are the possible menu letters, the columns are the 26 different voltages that make up the inputs and output of the scramblers.

I also don’t know if this is the best way to do it. I am sure there are better, more efficient ways. But I haven’t seen any actual code online or even any algorithms explained so this is what I came up with. If you can do better knock yourself out, feel free to take this code and play with it and improve it. In the code and explanation I talk about currents and voltages interchangeably. I just mean there is a signal present at a certain point. In practical terms whether it is a voltage or a current depends on how you measure it!

OK, the code explained in the order it follows to run (more or less). The actual code is at the bottom.

Lines 100 – 210:

First the drums which represent the Enigma rotors. I have represented the Enigma rotors in my Bombe as arrays. My Bombe only uses the 5 Enigma rotors. Each array is in two parts to handle the double endedness of the Bombe drums. The first half is a direct lookup so input on input x give output y. The second half gives the reverse. With just one array the forward direction is easy, the input letter is the index into the array. In the reverse direction you would have to scan the whole array to find the matching letter then output the index.  My double ended array means you can use the index in both directions, you just offset half way into the array for the reverse direction. That was a big speed improvement in Orwell BASIC.

Another small detail to note is that the Bombe drums were wired up incorrectly! The wiring positions are offset from what they should be. Drums I, II and III are one position out. Drum IV is 2 positions out and Drum V is three positions out. I store the offset for each drum then that is added or subtracted as necessary in the code. Basically it works the same as a ring setting would! Since the drums are hard coded in the setup subroutine (10000 onwards) these offsets are also hard coded.

The reflectors are also stored here as lookup arrays.

Lines 230 – 980:

Various variables are declared here. The important ones to note are the arrays for the scrambler offsets and the scrambler connections. This is one of the Quirks in MS BASIC. The code DIM SO$(12) will give you a 13 element array! The array is indexed from 0 to 12.

Similarly we have a 2 dimensional array to represent the diagonal board so it is declared like this: DIM DB(25,25) to give us a 26×26 array.

Lines 700 to 720 are for the menu. We maintain a list of all the letters in the menu and for each letter a list of what scramblers it is connected to.

Line 1000:

The main program starts here. The first thing we so is jump to the setup subroutine to configure the Bombe.

Lines 10000 onwards:

This is where the Bombe and menu are configured. Everything is hard coded and it is here you would need to modify things to run different menus. That isn’t difficult but you need a very good understanding of how to do this.

Lines 10010 – 10030:

Here we select the drums in use and specify their offsets. For the BP menu we are using drums II, V and III (which have offsets 1, 3 and 1).

Line 10040:

The reflector to use, reflector B.

Lines 10050 – 10170:

The scrambler offsets used in the menu for each scrambler. The order these are listed here is no important except that the order of the next array must match it.

Lines 10200 – 10320:

The menu letters each scrambler above is connected to. Here you must match the two arrays, i.e. the scrambler in position ZZK on the menu is between letters U and E on the menu.

Lines 10330 – 10340:

Here you specify the input value and the test register letter, in this case A and G. The test register is connected to the menu letter marked as input on the menu.

Line 10350:

This is where you can specify the starting offset. The Bombe will step once form this offset then start tracing so if you want to start tracing at DKX you specify EKX here. Remember the Bombe moves the top drum first and the indicator drums letters go backwards!

Lines 10360 – 10370:

These lines configure debug printing. Even though it is terribly slow I recommend turning on the diagonal board printing. This will output what is happening on the diagonal board as the machine runs and it really beautifully shows exactly what is happening in the machine. In a real machine this all happens almost instantaneously. In code we have to trace every single little current around the system by iterating through until we’ve traced as far as we can. This is why software Bombes are so slow compared to an original. It takes a lot of computer power to do it.

This is another thing the film misportrayed. The Bombe is not a Turing machine. It’s not any kind of computer at all. It’s just a (bloody complicated) continuity tester! I do find it amusing though that I’ve written a simulation of the non Turing machine Bombe on a machine that is basically a Turing machine (not quite though, it gets complicated).

Lines 10400 – 10520:

The menu letter array. This is just the letter in the menu. This array need to match the following array of menu letter connections.

Lines 10530 – 10710:

The array of which scrambler is connected to each menu letter. This array must match the array above.

Lines 1050 – 1460:

Printing of the setup data. Here I just output the various settings so they can be checked on screen before starting the Bombe run. Here we also jump to a subroutine to offset all the scramblers if we have specified an offset in the setup. For each scrambler in turn we call the offset scrambler subroutine.

Lines 2800 – 2990:

The offset scrambler subroutine. If an offset was specified here we offset the drums of the scrambler that was passed in via the CS$ variable (current scrambler).

Lines 1800 – 1870:

Here we print out the scrambler settings to the screen after applying any offset.

Lines 5000 – 5520:

This is the main routine in the program. The PRINT CHR$(7) is the bell command. It makes the computer go Beep! The first thing we do is clear the diagonal board.

Lines 4700 – 4780:

Here we clear the diagonal board array. We also set the count of untraced voltages to 0. We do at the start of checking every drum position on the machine.

Lines 4000 – 4200:

These two subroutines set values on the diagonal board. This is a very important function and is called constantly as currents are traced around the system.

For each input letter we check we set not only it’s own value but it’s diagonal partner. It’s a simple 2 dimensional array so if we set (x,y) we also set (y,x). The rows represent the potential menu letters and the columns are the currents on those letters.

We do however set different values depending on the circumstance.

Line 5030. 

Here we set the initial starting current. We set the current on the given input letter (‘A’) on the given menu letter that the test register (‘G’) is connected to.

Next we increment the drums and decrement the indicator.

Lines 2700 – 2770:

The move drums subroutine. For each scrambler we call the increment drums subroutine to move the drums one position on and handle any turnovers.

Lines 3000 – 3300:

The increment drums subroutine. Here we increment each drum starting with the top (fastest) drum. If the top drum wraps around (i.e. has done one revolution) we increment the second drum. Similarly with the third drum if the second has done one full revolution.

This is where my Bombe differs for the mechanical Bombe. If you watch that very closely when it is running you’ll note that the top drum actually does one an half revolutions before the middle drum moves. I haven’t seen this mentioned in any of the Bombe books or documentation online. The reason is the real Bombe is a physical machine so they carry over takes some time. Since the top drum is rotating continuously by the time the second (and third) drums have moved it has already moved on some positions. Obviously we can’t trace currents while things are moving like that so the real Bombe has a half cycle wait built in. It waits half a cycle before starting to measure after each revolution. I have modelled that behaviour in my mechanical model of the Bombe and my C code is modified to handle it.

Lines 3500 – 3700:

Here we decrement the indicator drums.

Line 5080:

Here we stop the Bombe run if we have checked all possibilities. Remember if the start position of the indicators is ZZZ the machine steps once to take the first measurement, AZZ, so it’s after we have checked ZZZ that we are done.

Lines 5110 – 5200:

This is where we loop around tracing the currents for this drum position. We loop until there are zero untraced voltages then we check the test register to see where the currents went.

We do this for each letter in the menu (the ML$ array). For each letter in the menu we scan across the diagonal board for the 26 different positions. Anywhere we find an untraced voltage (a -1) we jump to the trace voltage subroutine.

Line 6000 – 6190:

This is the tricky bit. Here, given an untraced voltage on a menu letter we figure out where the current will flow too.

For each scrambler connected to this menu letter (the MC array) we send the given input current through the scrambler and set the appropriate output. The scrambler subroutine gives us the output current after it has been through the scrambler. We check that scramblers connected menu letter (the SC$ array) and set the current on the one that is the other end of the scrambler (i.e. this menu letter connects to a scrambler that connects to another menu letter). We only set that output current if the value isn’t already set.

The current setting is done via the diagonal board so if we are checking current on letter ‘a’ on menu letter ‘E’ which connects to menu letter ‘U’ via scrambler 1 and the scrambler 1 give an output of ‘s’ for an input of ‘a’ on the diagonal board we will set ‘s’ on ‘U’ and also ‘u’ on ‘S’. Now since ‘S’ is also a menu letter we would set the diagonal board to -1 if it is currently not already -1 or 1 (meaning we’d already traced it). If the scrambler had output ‘t’ when given ‘a’ as the input we would set on the diagonal board ‘t’ on menu letter ‘U’ and also set the diagonal ‘u’ and ‘T’. But since ‘T’ isn’t a menu letter we set it to 2 since we will never trace it. The 2 is just for display purposes really. That current will never be traced anyway since we only trace current through the menu letters.

See, simple! If you understood that (good luck!) you can now see why the diagonal board is so important.

Every time we set a new untraced current (a -1 on the diagonal board) we increment a count of the untraced currents. Every time we trace one we set it’s value to 1 on the diagonal board and decrement the count. When the count is zero we’ve finished tracing.

Lines 2000 – 2660:

Here we handle feeding and input into a scrambler and getting back the output. Basically these subroutines implement an Enigma machine without the plugboard.

Lines 5210 – 5310:

When there are no more voltages to trace we check the test register. This is a simple matter of scanning the appropriate letter on the diagonal board (‘G’) and counting up all the set voltages. If it is less than 26 we have a stop. If we have a stop we print out the result and stop running.

If all 26 voltages were set it isn’t a stop so we start again. We increment the drums, clear the diagonal board. Feed in the test voltage to the test register again and start tracing current.

That’s basically it! There is other code there to print out various things such as the test register. The diagonal board printing subroutine (lines 4300 – 4450) will print out the diagonal board in a screen pretty format. The following characters are used:

Here is the actual code below. To run this yourself you can use the online Applesoft BASIC emulator here. Copy and paste the code into there and then change line ‘1010 CLS’ to ‘1010 HOME’ (you can also just remove line 1010 completely). This will start running on the known stop DKX and you can see how it traces through the diagonal board.

Note that this code is very slow! Running on Orwell, which has a 1Mhz clock, it would take about 4.8 months to do a full Bombe run. The real Bombe does a run in about 20 minutes. My C code version, running on an Intel i5 at 2.2Ghz takes about 10 seconds.

 

100 REM "ABCDEFGHIJKLMNOPQRSTUVWXYZ ABCDEFGHIJKLMNOPQRSTUVWXYZ" 
110 R1$ = "EKMFLGDQVZNTOWYHXUSPAIBRCJ UWYGADFPVZBECKMTHXSLRINQOJ" : REM I
120 R2$ = "AJDKSIRUXBLHWTMCQGZNPYFVOE AJPCZWRLFBDKOTYUQGENHXMIVS" : REM II
130 R3$ = "BDFHJLCPRTXVZNYEIWGAKMUSQO TAGBPCSDQEUFVNZHYIXJWLRKOM" : REM III
140 R4$ = "ESOVPZJAYQUIRHXLNFTGKDCMWB HZWVARTNLGUPXQCEJMBSKDYOIF" : REM IV 
150 R5$ = "VZBRGITYUPSDNHLXAWMJQOFECK QCYLXWENFTZOSMVJUDKGIARPHB" : REM V

200 UB$ = "YRUHQSLDPXNGOKMIEBFZCWVJAT" : REM Reflector B
210 UC$ = "FVPJIAOYEDRZXWGCTKUQSBNMHL" : REM Reflector C

230 RF$ = " " : REM Bombe Reflector
240 D1$ = " " : REM Top Bombe drum
250 D2$ = " " : REM Middle Bombe drum
260 D3$ = " " : REM Bottom Bombe drum
270 DIM DO(2) : REM Current drum offsets
280 D = 0: REM Drum offset from ENIGMA rotor 

300 DIM SO$(12) : REM Scramblers relative offsets
310 DIM SC$(12) : REM Scramblers connections

400 DIM DB(25,25) : REM Letters array
410 L1$ = "" : REM Diagonal board letter 1
420 L2$ = "" : REM Diagonal board letter 2

500 ID$ = "ZZZ" : REM Indicator drums

600 UT = 0 : REM Untraced voltages

650 D1 = 0 : REM Drum 1 counter
660 D2 = 0 : REM Drum 2 counter
670 D3 = 0 : REM Drum 3 counter

700 ML = 0 : REM Number of menu letters
710 DIM ML$(12) : REM Menu letters array
720 DIM MC(12,5) : REM Menu connections array

800 IV = 0 : REM Input voltage letter
810 TR = 0 : REM Test menu letter

900 CS$ = "ZZZ" : REM Scrambler offset 
910 SV = 1 : REM Scrambler value
920 V$ = "" : REM Scrambler character
930 OD = 0 : REM Offset current scrambler drum
940 CD$ = "" : REM Current scrambler rotor
950 L$ = "" : REM Scrambler drum letter
960 L = 0 : REM Scrambler drum letter value
970 SL = 0 : REM Scramblers connected output letter
980 VC = 0 : REM Test register voltage count

1100 REM ---------- Main program ----------
1010 CLS
1020 REM Bombe setup
1030 PRINT "BOMBE SETUP DATA..." : PRINT
1040 GOSUB 10000

1050 PRINT "TOP DRUM: "; : CD$ = D1$ : GOSUB 1600
1060 PRINT "MIDDLE DRUM: "; : CD$ = D2$ : GOSUB 1600
1070 PRINT "BOTTOM DRUM: "; : CD$ = D3$ : GOSUB 1600
1080 PRINT "REFLECTOR: "; : GOSUB 1700

1090 PRINT "SCRAMBLERS: "
1100 GOSUB 1800 : REM Print scramblers
1110 PRINT 

1170 PRINT "NUMBER OF MENU LETTERS: "; : PRINT ML

1180 PRINT "MENU LETTERS: "
1190 FOR I = 0 TO ML - 1
1200 PRINT ML$(I); : PRINT ":";
1210 FOR J = 0 TO 5 
1220 IF MC(I, J) <> 0 THEN PRINT MC(I, J); : PRINT " ";
1230 NEXT J
1240 PRINT
1250 NEXT I
1260 PRINT 

1300 PRINT "INPUT VOLTAGE: "; : PRINT CHR$(IV+65);
1310 PRINT
1320 PRINT "INPUT STECKER LETTER: "; : PRINT CHR$(TR+65);
1330 PRINT

1350 PRINT "INDICATOR START: "; : PRINT ID$
1360 IF ID$ = "ZZZ" THEN GOTO 1450
1370 PRINT "OFFSETTING SCRAMBLERS:"
1380 FOR K = 0 TO 12
1390 IF LEN(SO$(K)) = 0 THEN GOTO 1440
1400 CS$ = SO$(K)
1410 GOSUB 2800 : REM Offset drums
1420 SO$(K) = CS$
1430 NEXT K

1440 GOSUB 1800 : REM Print scramblers
1450 GOSUB 5000 : REM Solve subroutine.
1460 END

1600 REM ---------- Print drum number subroutine ----------
1610 IF CD$ = R1$ THEN PRINT "I"
1620 IF CD$ = R2$ THEN PRINT "II"
1630 IF CD$ = R3$ THEN PRINT "III"
1640 IF CD$ = R4$ THEN PRINT "IV"
1650 IF CD$ = R5$ THEN PRINT "V"
1660 RETURN

1700 REM ---------- Print reflector subroutine ----------
1710 IF RF$ = UB$ THEN PRINT "B"
1720 IF RF$ = UC$ THEN PRINT "C"
1730 RETURN

1800 REM ---------- Print scrambler subroutine ----------
1810 FOR K = 0 TO 12
1820 PRINT SO$(K); : PRINT ":"; : PRINT SC$(K);
1830 IF K = 4 THEN PRINT : GOTO 1860
1840 IF K = 9 THEN PRINT : GOTO 1860 
1850 PRINT " ";
1860 NEXT K
1870 RETURN

2000 REM ---------- Scrambler subroutine ----------
2005 IF P2 = 1 THEN PRINT "INPUT: "; : PRINT CHR$(SV+64); 
2010 CD$ = D3$
2020 OD = ASC(MID$(CS$, 3, 1)) - 65
2030 D = DO(2)
2040 GOSUB 2400 : REM Calculate scrambler offset
2050 GOSUB 2500 : REM Forward through rotor

2060 CD$ = D2$
2070 OD = ASC(MID$(CS$, 2, 1)) - 65
2080 D = DO(1)
2090 GOSUB 2400 : REM Calculate scrambler offset
2100 GOSUB 2500 : REM Forward through rotor

2110 CD$ = D1$
2120 OD = ASC(MID$(CS$, 1, 1)) - 65
2130 D = DO(0)
2140 GOSUB 2400 : REM Calculate scrambler offset
2150 GOSUB 2500 : REM Forward through rotor

2160 V$ = MID$(RF$, SV, 1)
2170 SV = ASC(V$) - 64 
2175 IF P2 = 1 THEN PRINT V$;

2180 CD$ = D1$
2190 OD = ASC(MID$(CS$, 1, 1)) - 65
2200 D = DO(0)
2210 GOSUB 2400 : REM Calculate scrambler offset
2220 GOSUB 2600 : REM Back through rotor

2230 CD$ = D2$
2240 OD = ASC(MID$(CS$, 2, 1)) - 65
2250 D = DO(1)
2260 GOSUB 2400 : REM Calculate scrambler offset
2270 GOSUB 2600 : REM Back through rotor

2280 CD$ = D3$
2290 D = DO(2)
2300 OD = ASC(MID$(CS$, 3, 1)) - 65
2310 GOSUB 2400 : REM Calculate scrambler offset
2320 GOSUB 2600 : REM Back through rotor

2325 IF P2 = 1 THEN PRINT " OUTPUT: "; : PRINT V$ 
2330 RETURN

2350 REM ---------- Wrap scrambler offset subroutine ----------
2360 IF SV < 1 THEN SV = SV + 26 : GOTO 2360
2370 IF SV > 26 THEN SV = SV - 26 : GOTO 2370
2380 RETURN

2400 REM ---------- Scrambler offset subroutine ----------
2410 REM SV = letter, OD = drum offset, 25 = Z ring, D = bombe drum offset
2420 SV = SV + OD - 25 - D
2430 GOSUB 2350 : REM Handle wapping
2440 RETURN

2500 REM ---------- Forward through scrambler subroutine ----------
2510 V$ = MID$(CD$, SV, 1)
2520 REM SV = letter, OD = drum offset, 25 = Z ring, D = Bombe drum offset 
2530 SV = (ASC(V$) - 64) - OD + 25 + D
2540 GOSUB 2350 : REM Handle wapping
2550 V$ = CHR$(SV+64) 
2555 IF P2 = 1 THEN PRINT V$;
2560 RETURN

2600 REM ---------- Back through scrambler subroutine ----------
2610 V$ = MID$(CD$, SV + 27, 1)
2620 REM SV = letter, OD = drum offset, 25 = Z ring, D = Bombe drum offset 
2630 SV = (ASC(V$) - 64) - OD + 25 + D
2640 GOSUB 2350 : REM Handle wapping
2650 V$ = CHR$(SV+64)
2655 IF P2 = 1 THEN PRINT V$;
2660 RETURN

2700 REM ---------- Move drums subroutine ----------
2710 FOR I = 0 TO 12
2720 IF LEN(SO$(I)) = 0 THEN GOTO 2760
2730 CS$ = SO$(I)
2740 GOSUB 3000
2750 SO$(I) = CS$
2760 NEXT I
2770 RETURN

2800 REM ---------- Set scrambler offset subroutine ----------
2810 L = ASC(LEFT$(CS$, 1))
2820 L = L + (26 - (ASC(LEFT$(ID$, 1)) - 64))
2830 IF L < 65 THEN L = L + 26 
2840 IF L > 90 THEN L = L - 26 
2850 L$ = CHR$(L) 
2860 CS$ = L$ + RIGHT$(CS$, 2)

2870 L = ASC(MID$(CS$, 2, 1))
2880 L = L + (26 - (ASC(MID$(ID$, 2, 1)) - 64))
2890 IF L < 65 THEN L = L + 26
2900 IF L > 90 THEN L = L - 26
2910 L$ = CHR$(L)
2920 CS$ = LEFT$(CS$, 1) + L$ + RIGHT$(CS$, 1)

2930 L = ASC(RIGHT$(CS$, 1))
2940 L = L + (26 - (ASC(RIGHT$(ID$, 1)) - 64))
2950 IF L < 65 THEN L = L + 26
2960 IF L > 90 THEN L = L - 26
2970 L$ = CHR$(L)
2980 CS$ = LEFT$(CS$,2) + L$
2990 RETURN

3000 REM ---------- Increment scrambler offset subroutine ----------
3010 L$ = LEFT$(CS$, 1)
3020 L = ASC(L$)
3030 L = L + 1 
3040 IF L > 90 THEN L = 65 
3050 L$ = CHR$(L) 
3060 CS$ = L$ + RIGHT$(CS$, 2)
3070 D1 = D1 + 1
3080 IF D1 < 26 THEN GOTO 3300
3090 D1 = 0

3100 L$ = MID$(CS$, 2, 1)
3110 L = ASC(L$)
3120 L = L + 1
3130 IF L > 90 THEN L = 65
3140 L$ = CHR$(L)
3150 CS$ = LEFT$(CS$, 1) + L$ + RIGHT$(CS$, 1)
3160 D2 = D2 + 1
3170 IF D2 < 26 THEN GOTO 3300
3180 D2 = 0

3200 L$ = RIGHT$(CS$, 1)
3210 L = ASC(L$)
3220 L = L + 1
3230 IF L > 90 THEN L = 65
3240 L$ = CHR$(L)
3250 CS$ = LEFT$(CS$,2) + L$
3260 D3 = D3 + 1
3270 IF D3 < 26 THEN GOTO 3300
3280 D3 = 0
3300 RETURN

3500 REM ---------- Decrement indicator drums subroutine ----------
3510 L$ = LEFT$(ID$, 1)
3520 L = ASC(L$)
3530 L = L - 1 
3540 IF L < 65 THEN L = 90 
3550 L$ = CHR$(L) 
3560 ID$ = L$ + RIGHT$(ID$, 2)
3570 IF L$ <> "Z" THEN GOTO 3800

3600 L$ = MID$(ID$, 2, 1)
3610 L = ASC(L$)
3620 L = L - 1
3630 IF L < 65 THEN L = 90
3640 L$ = CHR$(L)
3650 ID$ = LEFT$(ID$, 1) + L$ + RIGHT$(ID$, 1)
3660 IF L$ <> "Z" THEN GOTO 3800

3700 L$ = RIGHT$(ID$, 1)
3710 L = ASC(L$)
3720 L = L - 1
3730 IF L < 65 THEN L = 90
3740 L$ = CHR$(L)
3750 ID$ = LEFT$(ID$,2) + L$
3800 RETURN

4000 REM ---------- Diagonal board subroutine ----------
4010 IF DB(ASC(L1$) - 65, ASC(L2$) - 65) = 0 THEN GOSUB 4100
4020 IF L1$ = L2$ THEN GOTO 4050
4030 T$ = L1$ : L1$ = L2$: L2$ = T$
4040 IF DB(ASC(L1$) - 65, ASC(L2$) - 65) = 0 THEN GOSUB 4100
4050 RETURN

4100 REM ---------- Set value subroutine ---------- 
4120 FOR I2 = 0 TO ML 
4130 IF L1$ = ML$(I2) THEN GOTO 4200
4140 NEXT I2
4150 DB(ASC(L1$) - 65, ASC(L2$) - 65) = 2
4160 RETURN
4200 DB(ASC(L1$) - 65, ASC(L2$) - 65) = -1
4205 UT = UT + 1
4210 RETURN

4300 REM ---------- Print diagonal board ----------
4310 PRINT;
4320 PRINT " ";: FOR I1 = 0 TO 25
4330 PRINT CHR$(I1+65);
4340 NEXT I1 : PRINT
4350 FOR I1 = 0 TO 25
4360 FOR J1 = 0 TO 25
4370 IF J1 = 0 THEN GOTO 4440
4380 IF DB(I1,J1) = 0 THEN PRINT " ";
4382 IF DB(I1,J1) = 1 THEN PRINT "|";
4384 IF DB(I1,J1) = -1 THEN PRINT "x";
4385 IF DB(I1,J1) = 2 THEN PRINT "o";
4390 NEXT J1
4400 PRINT
4410 NEXT I1
4420 PRINT
4430 RETURN
4440 IF I1 = TR THEN PRINT " "; : GOTO 4380
4450 PRINT CHR$(I1+65); : GOTO 4380

4500 REM ---------- Print test register ----------
4510 PRINT "TEST REGISTER: " 
4520 FOR I1 = 0 TO 25
4530 PRINT CHR$(I1+65); 
4540 NEXT I1 : PRINT
4550 FOR I2 = 0 TO 25
4560 IF DB(TR,I2) = 1 THEN PRINT "|";
4570 IF DB(TR,I2) <> 1 THEN PRINT " ";
4580 NEXT I2
4590 RETURN

4700 REM ---------- Clear diagonal board ----------
4710 FOR I1 = 0 TO 25
4720 FOR J1 = 0 TO 25
4730 DB(I1,J1) = 0
4740 NEXT J1
4760 NEXT I1
4770 UT = 0 : REM Clear the untraced count
4780 RETURN

5000 REM ---------- Solve subroutine ----------
5010 PRINT CHR$(7)
5020 GOSUB 4700 : REM Clear diagonal board
5030 L1$ = CHR$(IV + 65) : L2$ = CHR$(TR + 65) : GOSUB 4000

5050 GOSUB 2700 : REM Increment drums
5060 GOSUB 3500 : REM Decrement indicator 
5070 PRINT :PRINT : PRINT "INDICATOR: "; : PRINT ID$
5080 IF ID$ = "ZZZ" THEN GOTO 5500
5090 IF P1 = 1 THEN PRINT "SCRAMBLERS: "
5100 IF P1 = 1 THEN GOSUB 1800 : REM Print scramblers

5110 FOR I = 0 TO ML - 1 : REM For each menu letter
5115 IF UT = 0 THEN GOTO 5210 : REM Loop until no voltage untraced
5120 IF P1 = 1 THEN PRINT
5130 IF P1 = 1 THEN PRINT "CHECKING LETTER: "; : PRINT ML$(I)
5135 IF P1 = 1 THEN PRINT "Untraced: "; : PRINT UT
5140 IF P3 = 1 THEN GOSUB 4300 : REM Print diagonal board
5160 FOR J = 0 TO 25 : REM Check each voltage on this letter
5170 IF DB(ASC(ML$(I))-65,J) = -1 THEN GOSUB 6000
5180 NEXT J
5190 NEXT I
5200 GOTO 5110

5210 REM Check test register
5260 VC = 0
5270 FOR I2 = 0 TO 25
5280 IF DB(TR,I2) = 1 THEN VC = VC + 1
5290 NEXT I2
5300 IF VC < 26 GOTO 5400 : REM A stop!
5310 GOTO 5020 : REM All voltages traced

5400 PRINT : PRINT "STOP" : PRINT CHR$(7)
5410 PRINT "INDICATOR: "; : PRINT ID$
5420 GOSUB 4500 : PRINT : REM Print test register
5430 REM STOP
5440 GOTO 5020

5500 PRINT "BOMB RUN COMPLETE" 
5510 PRINT CHR$(7)
5520 END

6000 REM ---------- Trace voltage ----------
6010 DB(ASC(ML$(I))-65, J) = 1 : UT = UT - 1
6020 FOR K = 0 TO 5
6030 REM For each connected scrambler 
6040 IF MC(I,K) = 0 THEN RETURN
6050 REM Setup the scrambler
6060 CS$ = SO$(MC(I,K)-1)
6070 SV = J + 1 : REM Input letter
6080 GOSUB 2000 : REM Through scrambler
6090 IF LEFT$(SC$((MC(I,K)-1)),1) <> ML$(I) THEN GOTO 6100
6095 SL = ASC(RIGHT$(SC$((MC(I,K)-1)),1))-65 : GOTO 6110
6100 SL = ASC(LEFT$(SC$((MC(I,K)-1)),1))-65
6110 IF DB(SL,SV-1)=-1 THEN GOTO 6180
6120 IF DB(SL,SV-1)=1 GOTO 6180
6140 REM Feed into diagonal board
6150 L1$ = CHR$(SL+65)
6160 L2$ = CHR$(SV-1+65)
6170 GOSUB 4000 : REM Diagonal board
6180 NEXT K
6190 RETURN

6200 REM ---------- Print debugging subroutine ----------
6220 PRINT MC(I,K); : PRINT" ";
6230 GOSUB 6500 : PRINT":";
6240 PRINT CHR$(J+65); : PRINT"-"; 
6250 PRINT CHR$(SV+64); : PRINT" ";
6260 PRINT CHR$(SL+65); : PRINT 
6270 RETURN

6500 REM ---------- Print drum ----------
6510 FOR K1 = 1 TO 3
6520 L = ASC(MID$(CS$,K1,1))
6530 L = L - DO(K1-1)
6540 IF L < 65 THEN L = L + 26
6550 IF L > 90 THEN L = L - 26 
6560 PRINT CHR$(L);
6570 NEXT K1
6580 PRINT " ";
6590 RETURN

10000 REM ---------- Setup subroutine ----------

10010 D1$ = R2$ : DO(0) = 1 : REM Rotor 2
10020 D2$ = R5$ : DO(1) = 3 : REM Rotor 5
10030 D3$ = R3$ : DO(2) = 1 : REM Rotor 3

10040 RF$ = UB$ : REM Reflector B

10050 SO$(0) = "ZZK"
10060 SO$(1) = "ZZE"
10070 SO$(2) = "ZZF"
10080 SO$(3) = "ZZN"
10090 SO$(4) = "ZZM"
10100 SO$(5) = "ZZG"
10110 SO$(6) = "ZZP"
10120 SO$(7) = "ZZB"
10130 SO$(8) = "ZZJ"
10140 SO$(9) = "ZZI"
10150 SO$(10) = "ZZL"
10160 SO$(11) = "ZZO"
10170 SO$(12) = "ZZA"

10200 SC$(0) = "UE"
10210 SC$(1) = "EG"
10220 SC$(2) = "GR"
10230 SC$(3) = "RA"
10240 SC$(4) = "AS"
10250 SC$(5) = "SV"
10260 SC$(6) = "EV"
10270 SC$(7) = "EN"
10280 SC$(8) = "HZ"
10290 SC$(9) = "RZ"
10300 SC$(10) = "GR"
10310 SC$(11) = "GL"
10320 SC$(12) = "SW" 

10330 IL = 0 : REM Input letter value - A
10340 TR = 6 : REM Test register value - G

10350 ID$ = "EKX" : REM Indicator offset

10360 P1 = 1 : REM Debug printing 0 = off
10370 P2 = 0 : REM Enigma printing 0 = off
10380 P3 = 0 : REM Diagonal board printing 0 = off

10400 ML$(0) = "U"
10410 ML$(1) = "E"
10420 ML$(2) = "G"
10430 ML$(3) = "R"
10440 ML$(4) = "A"
10450 ML$(5) = "S"
10460 ML$(6) = "V"
10470 ML$(7) = "N"
10480 ML$(8) = "H"
10490 ML$(9) = "Z"
10500 ML$(10) = "L"
10510 ML$(11) = "W" 
10520 ML = 12 : REM Number of menu letters

10530 FOR I = 0 TO ML - 1 : REM Menu letter connections
10540 FOR J = 0 TO 5 : REM Connections for letter
10550 READ S
10560 MC(I, J) = S
10570 NEXT J
10580 NEXT I

10600 DATA 1, 0, 0, 0, 0, 0 : REM 'U'
10610 DATA 1, 2, 7, 8, 0, 0 : REM 'E'
10620 DATA 2, 3, 11, 12, 0, 0 : REM 'G'
10630 DATA 3, 4, 10, 11, 0, 0 : REM 'R'
10640 DATA 4, 5, 0, 0, 0, 0 : REM 'A'
10650 DATA 5, 6, 0, 0, 0, 0 : REM 'S'
10660 DATA 6, 7, 0, 0, 0, 0 : REM 'V'
10670 DATA 8, 0, 0, 0, 0, 0 : REM 'N'
10680 DATA 9, 0, 0, 0, 0, 0 : REM 'H'
10690 DATA 9, 10, 0, 0, 0, 0 : REM 'Z'
10700 DATA 12, 0, 0, 0, 0, 0 : REM 'L'
10710 DATA 13, 0, 0, 0, 0, 0 : REM 'W'

20000 RETURN


Next Page »