Friday, 12 April 2019

Plottastic ZX80!

A guide to plotting pixels in Basic on a ZX80!


Introduction

Both the ZX80 and ZX81 support 8x8 pixel character-only displays and contain 16 graphic characters that can be used to plot fat 4x4 pixel pixels on a two-by-two grid within each character:

These are called Battenberg graphics after the cake of the same name 😀

On a ZX81 it's easy to use these characters to plot (crude) graphics on the screen, the computer provides a PLOT x,y and UNPLOT x,y for this purpose. But with half the ROM on a ZX80, it's much harder - so I wondered, how much harder is it to plot pixels? It turns out it's pretty formidable!

Challenges


  • The ZX80 doesn't have any PLOT or UNPLOT commands.
  • The screen on a ZX80 is like that on a ZX81, when you run a program, the screen first collapses to a minimum of 25 newline characters and expands as you display text. However, on a ZX80, unlike the ZX81, you can't print anywhere on the screen as there's no PRINT AT function, this means we'll have to poke onto the screen.
  • The memory map on a ZX81 has the display immediately after the program, but on a ZX80, the display comes after the variables and the editing workspace which means that it'll move around just by creating a new variable or possibly by performing input (which is a potential problem).
  • Ideally, to convert from pixel coordinates to Battenberg graphics you'd want to map the odd and even x and y coordinates to successive bit positions to index the character.


  • But, unlike the ZX81, the character set on a ZX80 doesn't have the characters in the right order to display Battenberg characters. Instead they contain gaps; the last 8 codes are generated from the first 8 but in reverse order; and some of the first 8 are taken from what ought to be the inverse characters!

The Solution

The solution really comes in a few parts. Firstly, the easiest way to be able to map an ideal pixel value to its Battenberg character is to create a table in RAM, by putting them into a REM statement (the ZX80 has no READ and DATA commands so it's hard to put a table of data into an array, but the first REM statement in a program is at a fixed location). However, even this presents a problem, because only 8 of the graphics characters can be typed. The easiest way to handle that is to write a program which converts a different representation of the characters into the correct ones.

So, on the ZX80, first type this:
After you run it, you'll get this:
Even this was tricky; I had to use character codes above 32, since symbols such as +, -, /, ", etc get translated into keyword codes for the Basic interpreter. The above program illustrates an interesting feature of ZX80 Basic that differs from ZX81 and ZX Spectrum Basic in that AND and OR operations are actually bitwise operations rather than short-circuit boolean operations. Thus P AND 15 masks in only the bottom 4 bits.

Once we have generated the symbols we can delete lines 10 to 30.

The next step is to actually write the code to generate pixels and plot them. Once we know how, it's actually a bit simpler. Firstly, we fill out the screen with spaces (or in my case, with '.'):
This gives us 20, full lines of characters. Because it's going to be difficult to plot a pixel by reading the screen, figuring out the plotted pixels then incorporating the new pixel; I cache a single character in the variable P and its location in the variable L. All my variables a single letters to save space if you try to run it on a 1Kb ZX80. The idea is that if the new print location in L changes, we reset P back to 0, otherwise we incorporate the new pixel.

Next we start the loop and calculations for plotting, in this case a parabola. We loop X=0 to 63 and calculate Y on each loop (it's a parabola that starts at the bottom of the screen):

Finally we perform the pixel plotting and loop round.

This involves saving the previous location in K so we can compare it later; then calculating the new location based on X and Y (since each character position is 2 pixels, we need to divide X and Y by 2 and since there are 32 visible characters and an invisible NEWLINE character on every screen line we must multiply Y/2 by 33). Note, a quirk of ZX80 Basic means that multiplies happen before divides, so Y/2*33 would calculate Y/66!

The pixel bit calculation in line 50 makes use of ZX80 bitwise operators, we want to generate 1 if X=0 (so 1+(X AND 1) will generate either 1 or 2) and then we need to multiply that by the effect of the Y coordinate, which is 1 on the top line, and 4 on the bottom: 1+(Y AND 1)*3 will do that. Hence this will generate the values 1, 2, 4, 8 depending on bit 0 of X, Y.

We must POKE the location L plus an offset of 1 (because the first character of the display is a NEWLINE) and also we must add the location of the display file (it turns out that these calculations don't make the display file move around). We poke it with the pixel value we want indexed into the right character code from the REM statement. Finally we loop round X.

This code generates this graph:
It looks reasonable even though it's generated with integer arithmetic. It's the first pixel plotted graph I've ever seen on a ZX80!

More Examples

My original reason for doing this was to see if I could generate sine waves using a simple method I once found for a Jupiter Ace. Here's the program and the result:
It looks fairly convincing, especially as it's all done with integer arithmetic. Because the code generates both the sine and cosine value, it's easy to turn this into a circle drawing program which produces the following:

It looks a bit odd, that's because the algorithm doesn't quite generate sine and cosine curves. What it's really doing is computing a rotation matrix, but only one of the terms is computed for sine and cosine each time. Hence, the circle looks a bit like an oval with a slight eccentricity.

My graph drawing algorithm has one very serious limitation. Because it doesn't read the screen in order to compute a new pixel, if the graph goes over the same part of the screen twice, the second pass will muck up what was there before. Doing the correct calculations is possible using some table lookups and bitwise operations, though it would slow down the graph generation. I didn't bother, because I only wanted to generate simple graphs.

Conclusion

The ZX80 and ZX81 have very similar hardware, but a number of design decisions in the ZX80's firmware made drawing circles much harder than you might expect. With a lot of effort it is possible to generate some simple graphs 😀

Saturday, 3 November 2018

The Gift of Sisyphus

George Monbiot always writes great articles on sustainability (and although this one is 6 years old it holds up well). Here, what George Monbiot is saying is that (as well as the abusive consequences of extractivism) about 50% of CO2 production is due to this kind of consumerism.

However, I would argue that the priority for everyone is simply to stop using fossil fuels asap. Imagine the same argument if we already had 100% clean energy: Monbiot would be saying that this pointless conumerism causes 0% of CO2 production. So, it would still be pointless, extractivist, and abusive, but it won't fundamentally be raising global temperatures for thousands of years.

The key thing to remember is that it's the total amount of CO2 emissions since the industrial revolution that matters, not the rate we emit. So, Reduce/ Re-use / Recycle (RRR) by itself won't fix global warming if we still use fossil fuel energy at all: it's simply like a bath that's trickling instead of gushing - it'll still overflow eventually.

But eliminating our fossil fuel usage and employing RRR makes it easier and quicker to get to 0 emissions. This still means the priority is clean energy now.

It's possible for most of us to cut out around 66+% of our domestic CO2 production by making just a few big decisions that keep paying both us and the world back. Listed in order of lowest to highest investment:

  1. Switch over to a 100% renewable energy supplier, like Ecotricity, or Good Energy. Pick a big supplier that actually owns Wind turbines, solar power and is starting to invest in baseload energy storage. This will cut out 30% of your emissions.
  2. Radically switch over any gas appliances to electricity. This will probably take a few years, but will include your gas fire and cooker / oven. This will probably address another 15% of your emissions (up to 45% now).
  3. Buy solar power for your house or invest in an Energy4All renewable cooperative. This will make other people's decisions for them, by removing some of the market for the existing fossil fuel industry and placing energy production power in your hands rather than theirs.
  4. Switch to an electric vehicle as soon as you can afford it. This will cut out another 30% of CO2 emissions. Even if you cut down on car driving and cycle or walk instead (a good thing), it's a false economy to believe that it's less polluting to keep an old fossil fuel car going as long as possible, because all it does is delay the time when you go clean: because the emissions involved in the new car will be a fixed cost. Every year we delay on this is a year the fossil fuel car industry has less incentive to switch: vote with your bank account.

The total here amounts to about 75% of your CO2 production and up to 30% of other's CO2 production. I cover this in a bit more detail in this post.

By contrast, difficulty with putting Monbiot's argument into practice is that to address it, we have to change our lifestyles in hundreds of small ways and a few big ways:

  1. To eliminate the consumerism we generate (though it's hard to predict when we'll stop using new things and there are also multiple ways of addressing consumerism, e.g. sharing as many consumables as possible).
  2. To eliminate the consumerism others generate for us (e.g gifts) without appearing to be kill-joys.
  3. To campaign for governments and world organisations to address the root causes, like conflict minerals. This is needed, because time and time again we're going to be suckered by cheaper products that we think we do need, and we don't have the time or knowledge to fully (or even partially) analyse the supply chains and ethics involved. Regulations are needed to shift the playing field in favour. Consider the effort involved by the company Fairphone (I have a fairphone 1) in tracing mineral sources.

So, the probability is that we'll try to do these things for a while, but slowly get overwhelmed by the market forces pushing us in the opposite direction.

This is really just an application of Every Big Helps from Sustainability Without the Hot Air.

Sunday, 8 July 2018

Three steps to Carbon Free Me (part 1)

Introduction

Recently I posted a comment to my cousin's post about the heatwave:

Ok, so no comments on the elephant in the room here. It’s not just us, much of the world is in the grip of a heatwave right now. 33 people have died in Quebec. We need to start addressing the underlying causes - talking about it is a good start. And it will help us if we understand this will get worse. In a few decades, this will be a normal summer. Sorry I bang on about it all the time.

Citing this Guardian article:


And she replied:

I think people (including myself) don't know what to say because it's such a big issue that we feel quite helpless about it... I don't think the majority are in denial that climate change is a real danger. It's more just that it feels so far out of our control that it's hard to think about it.. "10 things you can do now to reduce your impact on climate change" type articles would probably get more people on board

Sigh, I can really see her point. So, this is my first shot at an article like that.

Background

I think, to be able to respond at a personal level is to be clear about what the plan is. If we don't know what we're aiming for we won't know what to do and wouldn't even know how successful we've been. So, the goal here is very, very simple. It's not about plastic or planting trees, or a million things you can do that make a small difference. it's about relatively easy things you can do that will make literally a huge difference, like 30% to 60% of the whole way, for you.

The plan, globally, is to cut down on the emissions of CO2. To Zero. You may have heard talk of a low-carbon economy. A low-carbon economy will still be a disaster. Globally we have to cut down to nothing and then beyond. We have roughly 50 years to do this and in developing countries we have 30 years.

The positive news is that can cut out the majority of this or a substantial fraction of 30% to 60% pretty quickly. Cutting out 30% jumps us into the 2030s; cutting out 60% jumps us into the 2040s and we can feasibly do this in a fraction of the time. And the great thing about that is that the sooner we make these steps, the more breathing room we'll have along with other benefits. The principle here is "Every Big Helps".

This is a plan for domestic emissions.

Domestic emissions fall into 3, roughly similar sections: Electricity, Heating and Transport.


I obtained these values from some simple searching on the internet with phrases like "average electricity UK per year" etc. I'll update with proper links later, but for now, I found this CO2 calculator web page handy.

The total is: 7084Kg, 7 Tonnes for an average household! Wow! But a few decades ago in the UK it was 10 tonnes, and even a decade ago it was noticeably higher, so we've progressed. Let's deal with this from the easiest to the hardest. But before that...

Politics

We can't divorce politics from choices about energy. This should be obvious. Energy gives us (quite literally) power. Globally, 80% of that power currently comes from the fossil fuel industry. To put it another way, 80% of global power resides with the fossil fuel industry, an industry that has spent decades covering up the fact that their product is adding a blanket of heat to the world. To decarbonise means deliberately taking power away from them.

Here's an example, of how much power they have. The Church of England has a mission statement that says we have to look after the earth. It's called Stewardship. It means we think it's wrong to wreck God's creation, because God thinks his creation (which includes us) is good.

However, the Anglican church also has over £100 million pounds of investments in the fossil fuel industry and some sectors of that church have realised that's in conflict with the mission statement (really?). One of the most promising Christian movements to resolve this is Bright Now, which campaigns for churches to divest from fossil fuels.

So, in February 2014, in the Church's global get-together (the General Synod), they tabled a motion to promise a debate on divestment for the following year's Synod and in the meantime, they'd divest from the Alberta Tar Sands investments, the worst fossil fuels in the world, an industry so bad that the eminent ex-Nasa climate scientist, James Hansen said if the pipelines go ahead it's game over for the climate. So, the CofE divested from the worst of worst in 2015. Good on them - that's the way to show leadership, and I'm sure it had nothing to do with the crash in oil prices in 2014, making tar sands completely uneconomical.

Then I found out in early 2015 that the debate on divestment was being replaced by a debate on engagement with the fossil fuel industry, i.e. a commitment to the fossil fuel industry - with a wagging investment finger. How did this happen? Well, I found out directly from one of the key members of the Ethical Investment Advisory Group, Richard Burridge at a fringe meeting at that 2015 Synod in York. He explained that after the resolution in 2014, Rex Tillerson personally phoned them up to propose an alternative arrangement. And then the resolution was dropped. What this means is Rex Tillerson although not a member of the CofE, has more power over it than the Synod itself. Then after the meeting, in conversation, Richard Burridge started singing from the fossil fuel industry hymn sheet, explaining why divestment itself was bad for everyone and why developing countries needed fossil fuels. It's not, as Katherine Hayhoe explains.

Never underestimate the deviousness of the fossil fuel industry.

Efficiency

This blog ignores efficiency. That's because people concentrate on that so much I figure you probably know about some efficiency issues: LCD TVs, LED lights, that kind of thing. But also, because I want to focus on the key issue: efficiency by itself won't lead us to our zero-carbon future; we have to actively switch our energy sources. Also, I want to concentrate on the stuff that makes a huge difference: a huge difference is a big psychological boost as well as a practical action.

Actions

Step 1: Electricity Provider

By far the easiest way to cut down on your emissions is to choose a renewable energy company for your electricity. All it involves is switching supplier. It requires no investment and because renewable energy is getting cheaper thanks to economy of scale, these suppliers are relatively and increasingly competitive. The three main 100% renewable electricity suppliers in the UK are:

  1. Ecotricity - run by the visionary Dale Vince. They have their own (onshore) wind and solar farms. They also source renewable energy from overseas to meet demand.
  2. Good Energy - a well-established 100% renewable energy supplier and they're great for feed-in-tariff support.
  3. Cooperative Energy - their green pioneer electricity tariff is now 100% renewable. They have their own wind and solar farms.
There are other pop-up renewable companies such as bulb, but until you know where they get their renewable energy from I wouldn't recommend them, though they may be perfectly fine.

For an average household this will remove 23% of your emissions in one go. This takes you to November 2025 (if we lowered emissions steadily - in reality it'll take us a bit further than that, because it will be a curve).

Step 2: Heating and Cooking

The next easiest thing to do is to replace as many heating and cooking devices with electrical ones as the opportunity arises. For example, if you have the money, replace a gas fire with an electric fire. It won't be as warm, since they're limited to 2KW, but if you can tolerate it; then that's a way to go.

Also replace gas cookers with ceramic or induction electric cookers.

This is different to most of the advice you'll see, because they'll be comparing the CO2 emissions of an average electricity supplier with gas, but we are comparing renewable energy heating and cooking with gas. It will be more expensive, since electricity per KWh is more expensive than gas; so this requires an initial outlay, and an ongoing cost. We haven't found a massive increase in our electricity bills as a result.

Also, when I get figures for this, I'll update it in this blog post. Until then I'll assume that - based on these two items alone, you can save maybe 50% of gas emissions: 1565Kg of CO2, nearly as much as for electricity itself.

This would take 45% of the way, to the year: 2032.

Step 3: Transport

The next 'easiest' thing to do is to switch to electric vehicles as soon as you get a chance. An EV, running from a renewable electricity supply will save 100% of your transport emissions. Switching to electric transport involves a cost outlay, but the costs of running an EV are less than for a fossil fuel car, so it will pay back in the long term. There are many ways you can partially or fully achieve this and they're all based on the observation that most of our journeys are short.

  1. Buy an electric bicycle! Most EBs can suffice for journeys up to 45Km, enough for a visit to a friend, maybe a journey to work and back. They're limited to 15Km/h. Of course if you can handle a normal bicycle, then you'll get fitter too (but it won't save emissions, if you already have renewable electricity). Let's assume this deals with 30% of your journeys.
  2. If you have two cars, make one of them a second-hand EV and use it as the run-around: for shopping and shorter journeys. Second-hand EVs are increasingly available via auto-trader or ebay from about £5500 upwards. Most of them are first generation Nissan Leafs, some will be Zoes and BMW i3s; a few will be MiEv's (with a more limited range). These cars can carry a family on a journey 20 miles out and back, or 40 miles if you can charge it at your destination or will carry you on a 50 to 70 mile journey in total. Perhaps this will handle 70% of your journeys.
  3. If you're prepared to adapt your lifestyle, replace your car with an EV (100% of your journeys).
    1. For first generation EVs (like the 2016 Zoe we own), practical journeys will be in the 60 to 120 mile range (with one en-route re-charge) or 240 miles if you can charge at either end. We bought a second-user EV for £7500, but essentially it was new, so similar bargains may be possible. In essence, the vast majority of your journeys will fit these rules.
    2. For second generation EVs practical journeys will be in the range of 150 to 200+ miles either way. These vehicles start at £19K for the Zoe (with a leased battery); £20K for a Smart EQ; £25K or so for a new Leaf; about £34K for an i3; or similar for a Tesla model 3 (available from 2019), going up to £60K to £115K for a Tesla model S, Roadster or X.
With option 1, that saves: 700Kg of CO2 per year (assuming you use it consistently).

With option 2, it's 1623Kg of CO2 per year (assuming you use it consistently).

With option 3, you save 2319Kg of CO2 per year.

This takes us to a maximum potential of 78% of your emissions cut, ie. from 7084Kg to 1565Kg. It's equivalent to living in the year 2042. In other words, it gives us another 25 years to plan to eliminate the other 22%

Conclusion

This guide gives you actual figures. Things you can attain; that tell you roughly how far along the path to decarbonisation you've done. You can quote it in years so you know what year you're really living in. For me, that's a way to bring practical hope to the challenge.

Don't burn yourself out. It took us about 3 or 4 years to get as far as we have (which is about 2042), but we bought Solar Panels in the meantime and started on a mortgage. It might take you a bit longer, or you might have the resources to make major shifts now (the renewable energy shift is the easiest). Whatever you do, take a positive angle and encourage others to do the same :-)

Here's to a happy Carbon Free You!

Saturday, 13 January 2018

EV Intentionality

We have a cute Renault Zoe EV, called Evie as it happens, and when I get a chance at traffic lights I slip it into non-ECO mode so I can leave all the hotshot BMWs / Audis / Mercedes and Jaguars in the dust as I zoom away (within the speed limit of course :-) ) !

EV Intentionality is about driving and thinking EV in such a way as to convey the genuine benefits of the technology. They're the rapidly approaching future (fuel cell cars aren't) and we're in competition with the Fossil Fuel industry who are orders of magnitude bigger than us (until their stranded assets catch up with them ;-) ).

Friends frequently ask me if it's better to (a) buy an EV now, (b) buy a hybrid or (c) drive their current car into the ground. (c) Seems like common sense, but actually it's worse for the environment and your pocket. This is why (assumes average ICE car driving 12703Km/year at 120g/Km):
The way to look at it is to add up your emissions over the long-term. Put simply, buying an EV involves a one-time emissions hit (the production of the car, including the extraction of its raw materials) and after that, it can be emissions-free. This assumes you'll charge it on renewable energy, because we do.

Therefore every fossil fuel mile you add now, adds to your final emissions. By 2040, the EV bought in 2018 still has the same emissions, but the one bought in 2022, just 4 years later resulted in another 60% emissions and the one kept going until 2040 resulted in nearly 4x the emissions - before the EV was eventually bought).

Let's consider what happens if you buy a Hybrid (at 100g/Km) or a Plug-in Hybrid (PHEV) (at 45g/Km) or an EV vs driving the same ICE car for as long as possible:

Basically, the EV results in lower total emissions than continuing with your ICE by 2024 (in 6 years), the PHEV manages it by 2027 (in 9 years), and the Hybrid by 2040 (22 years), in other words, a long time after its life expectancy. Looking at the life expectancy (on average by 2032, given our start date); by then, the PHEV's total emissions are 75% more than the EV, but the Hybrid car has more total emissions than the ICE. In other words - you're very unlikely recoup the manufacturing emissions by buying a new Hybrid car compared with driving an existing ICE into the ground, though of course it'll be less emissions than buying a new ICE.

From an intentional viewpoint though we want to promote the transformation of transport. Consider:

  • Every fossil fuel mile you drive now, is a donation to the fossil fuel industry. They're not a charity. The first question to ask is "how much of my money do I want to give them?" If it's nothing (which is the right answer), then your basic decisions are made for you.
  • EVs will come down in price over time and improve faster over time. But the rate of this depends upon how quickly we switch. If we takes decades to go clean the rate of improvement will be much slower. It's what's called a market signal, which is a vote. You put your money in the market you have faith in and the market responds accordingly.
  • EV production will get cleaner over time as industrial practices decarbonise, but this will happen much slower than we can switch to EVs. By switching to EVs and running them on clean energy, we send another market signal, that we want a carbon-free lifestyle sooner. This is another market signal.
And given there seems to be at least one car advert on every commercial break on TV, we're going to have to be 100% intentional for the foreseeable future :-) !

[Edit: Graphs updated to include manufacturing footprints for EVs and ICE cars based on This Guardian Article. The article provides only EV and ICE footprints; I've estimated a Hybrid and PHEV manufacturing footprints based on typical CO2 emissions for the technologies on the basis that the battery technology and/or drivetrain is what contributes to the higher manufacturing emissions in proportion to the battery technology provided. In addition, I've assumed that manufacturing footprints will fall linearly until they fully decarbonise by 2070. These are provisional calculations until I get better information. Similarly, the study used to estimate EV manufacturing at 8.8Tonnes may assume a Tesla Model S as the standard EV, and that will not be representative across the globe - and it might not even be true for the Tesla Model S.]

Wednesday, 3 January 2018

A Listing As High As The Moon!

Did you know that the source code for the Apollo Guidance Computer would reach all the way to the moon if it was printed out?


No? Good - because it's not true and we'll do the math here. There's a whole world of computing mythology that's sprung up over the decades. A classic claim is that the computers on the Apollo spacecraft were less powerful than the computer inside a typical digital watch.

That means there's more software on that watch than you need to reach the Moon!

It's all rubbish. The Apollo Guidance Computer was relatively complex; it was a 16-bit machine that had 36KW of firmware on it and ran. It would take a typical software engineer years to write the code that filled it, and in fact it did take a team of talented software engineers (headed by Margaret Hamilton who coined the term software engineer) years to write its code - in assembler.

Fortunately we can see the source code for the AGC these days as it's on github. Based on the Wikipedia article and assuming each Timing Pulse is 1.024MHz, then instructions would typically take between 12 timing pulses, which gives 85.3 KIPs and most instructions might require twice as much as that (assuming a memory access took another 12 pulses), giving speeds between 42.7KIPs and 85.3KIPs (which can be verified here).

36KW of firmware is equivalent to about 72Kb of firmware. That means the computer could handle software about as complex as microcomputers from the year 1984, where the limits of 16-bit addressing and the shift from writing in assembler to writing in high level languages were being tested. Some microcomputers from that era did have more RAM (e.g. the ACT Sirius), but here I'm taking common home computers as the baseline.

When you look at the software in GitHub you find that there are a reasonable number of files, usually of a reasonable length. The source code there has a GitHub header of about 26 lines on every file, but the rest of it is the original software.

Let's first think about how much code might be on there. Each assembler line is probably 1 instruction, which is one word (though it might be more if they used a macro assembler). Typical listing paper had 66 lines per page, So, 36,864 words would be 36,864/66 = 558.5 pages. So, how thick is a page? You can still buy listing paper, and it works out at 24.1cm for 700 pages, so the whole listing is, at a minimum: 558.5/700*24.1cm = 19.22cm high, just under half-way up to my knee.

So, at a first guess, it's not very high. But I could be badly wrong, because the real software was different for the Command module and the Lunar module; and also they would have had many comments in the code, which would have lengthened it. So, I took a look at the actual source files.

Helpfully, they have page numbers in them. Luminary099, which was the Lunar module's software has 1510 pages, and the command module's software in Commanche055 had 1516 pages. The total is thus: 3026 pages which is: 114.5cm tall. That's a bit more like the diagram, but it still only goes up to my chest.

Again this might not quite be accurate, because some of the code could have been shared, I hope to post a future update when I've figured out how much is!

Sunday, 31 December 2017

Z180 MMu-tiny

There's surprisingly little in the way of clear explanations for Z180's MMU, which is mostly OK because as an 8-bit CPU, the Z180 isn't very popular any more.

So, here's a bit of an explanation. The Z180 is basically a Z80 with some extra built-in peripherals, one of which is a bank-switching MMU which provides the CPU with the ability to manage 1Mb of physical memory.

The MMU itself is very simplistic. It divides the normal 16-bit address space into 3 sections: a section (called Common 0) that's always mapped to the beginning of physical memory; a section (called the Banked Area) which can be mapped anywhere in the 1Mb of physical memory; and a final section (called the Common 1 area) which can also be mapped anywhere in the 1Mb of physical memory.



The two banked areas can be made to start at any 4Kb region in the logical 64Kb memory space; though the Common 1 area should be made to start after the Banked Area.

The MMU is controlled via 3 x 8-bit registers in I/O space, at addresses at 0x38 to 0x3A as follows:

CA and BA are both reset to 0xf, which means that only the top 4Kb is mapped on boot-up, and since BBR and CBR are reset to 0, then this means that the top 4Kb is mapped to 0x0f000, which is good because the MMU can't be switched off.

Zilog (and Hitachi who first implemented the scheme) intended the MMU to operate so that only the Bank Area would move during the lifetime of any Z180 process, which is why the top segment is called "Common Bank 1", rather than, say "Bank Area 2" (or '1' if we'd numbered the Bank Areas from 0).

But this scheme severely restricts the flexibility of the MMU. That's because a flexible banked system needs at least 4 moveable banks for a process. You need a non-moving banked area of data to hold the stack and key global variables. Similarly, you need a non-moving banked area of code to hold key common routines, e.g. the bank switching code or a vector to the OS. Then to be able to use the 1Mb space for larger applications you need a relocatable segment for code and another for data; which means 4 banks in total. The Z180 only provides 2 movable banks.

If I had designed the Z180 MMU I would have chosen a pure bank-switching technique where each virtually addressed 16Kb bank is independently mapped somewhere in the physical address space. What might that look like if we were confined to the equivalent set of registers as for the real Z180 MMU?

Here the registers are called the FBP (Fixed Bank Page), the DBP (the Data Bank page) and the CBP (the Code Bank Page).

Each register is 8-bits and they map the virtual address to a 22-bit virtual address space (providing 4Mb) as follows:

So, the Code and Data bank pages map to the first two 16Kb banks anywhere in the physical address space and the last 32Kb normally maps to a 32Kb bank. There are two use-cases for how we might want to do the mapping. Firstly, there's the general purpose OS case. Here, we assume that it's fairly easy to map fixed code and data to consecutive banks (moving a pre-allocated moveable bank if needed). Hence we can make do with a single bank register for this purpose. The second use-case is for embedded systems where there would be a single application (in ROM). Here, the X bit can be set so that the top 16Kb can be mapped into RAM in the other half of the physical address space.

The logic involved in this scheme would be at least as simple as the original Z180, because no adder is required to compute the virtual Address + the bank register and no comparison logic is needed to determine which segment any particular virtual address belongs to. Instead, the top two virtual address bits directly index the MMU registers.

One additional feature is implemented: a RST xx instruction, including RST 0 - reset, will clear the CBP. This means that it would be possible for an application or process running from banked or the fixed code bank to execute a ROM routine - in rather the same way it works with CP/M.

Thursday, 10 August 2017

Xenophobia on the Virgin Express

I've heard a lot about the growing Brexit xenophobia being nurtured by the likes of the Daily Mail, The Sun and Express, but prior to our holiday trip to the lovely North East town of Seahouses a couple of weeks ago I'd not really witnessed any.

I mean we've seen the video of anti-immigrant or racist abuse on the Manchester Metrolink right?

So, let's get down to the specifics. On Saturday, July 22 we were on the (I believe) Virgin Intercity (though it might have been Cross County) to Newcastle from Birmingham New Street. I think we would be arriving at 14:46 and then catching the next train to Berwick upon Tweed.

Me and my wife had an allocated pair of aisle seats at a table as it happened and there was a couple in the window seats too. I was travelling backwards. Slightly behind me and on the opposite side window seat was a lady - clearly very pregnant - enjoying the ride.

That is, until the conductor came along and asked for her ticket. Instead she presented him with a printed A4 sheet with her booking details including a code for the ticket. She didn't know this wasn't a proper ticket, because she was from Hungary and hadn't been in the country that long.

Instead of being gracious and explaining how she could get her ticket, the ticket inspector started to get really bolshy with her; threatening her with a fine or throwing her off the train - but she didn't have any real money with her either, her partner was the one who had actually bought the ticket online.

So, I said to the inspector it was obviously an innocent mistake so couldn't she get it sorted out at the end of the journey? To which he spouted "They wouldn't allow that where she comes from!" and stormed off down the carriage like a true Daily Express reader!

Me and my wife did a double-take. So, did the couple next to us at the window side. Then between us we all did a double-double take. Then we chatted with the woman for a few minutes and explained how she should be able to sort it out when she gets off the train by going to Information.

And the post-script for any xenophobe blog readers: I've no idea if they'd immediately chuck you off a train in Hungary for only having the booking information instead of an actual ticket, but who cares? Why should we define our behaviour by the worst we might imagine about another country's? To me it just sounds like a mirror image of your own inner thoughts and you can't fix that by targeting Eastern Europeans.