ebook img

Young Persons Guide to BCPL Programming on the Raspberry Pi PDF

677 Pages·2016·10.92 MB·English
by  
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Young Persons Guide to BCPL Programming on the Raspberry Pi

Young Persons Guide to BCPL Programming on the Raspberry Pi Part 1 by Martin Richards Contents Preface v 1 Setting up the Raspberry Pi 1 1.1 Later versions of the Raspberry Pi . . . . . . . . . . . . . . . . . 3 2 SD Card Initialisation 5 2.1 A More Recent SD Card Image . . . . . . . . . . . . . . . . . . . 10 2.2 NOOBS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 2.3 Raspberry Pi Model 3 . . . . . . . . . . . . . . . . . . . . . . . . 11 3 Introduction to Linux 13 3.1 The Filing System . . . . . . . . . . . . . . . . . . . . . . . . . . 15 3.2 The Desktop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 3.3 Midori . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 3.4 Editing Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 3.5 vi . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 3.6 emacs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 4 The BCPL Cintcode System 25 4.1 Installation of BCPL . . . . . . . . . . . . . . . . . . . . . . . . . 26 4.2 Hello World . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 4.3 Fibonacci . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 4.4 Multiplication Table . . . . . . . . . . . . . . . . . . . . . . . . . 42 4.5 A Mathematician’s Approach . . . . . . . . . . . . . . . . . . . . 43 4.6 Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 4.7 Applications of XOR and MOD . . . . . . . . . . . . . . . . . . . . . 50 4.7.1 RSA Mathematical Details . . . . . . . . . . . . . . . . . . 52 4.8 Vectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 4.9 Primes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 4.10 MANIFEST, GLOBAL and STATIC declarations . . . . . . . . . . . . . 58 4.11 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 4.12 Solving the recurrence relation for C . . . . . . . . . . . . . . . . 63 4.13 Greatest Common Divisor . . . . . . . . . . . . . . . . . . . . . . 64 ii CONTENTS iii 4.14 Powers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 4.15 Compilation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 4.16 The Collatz Conjecture . . . . . . . . . . . . . . . . . . . . . . . 72 4.17 The Pig Dice Game . . . . . . . . . . . . . . . . . . . . . . . . . . 78 4.17.1 The Optimum Strategy . . . . . . . . . . . . . . . . . . . . 86 4.18 The Enigma Machine . . . . . . . . . . . . . . . . . . . . . . . . . 89 4.18.1 enigma-m3 functions . . . . . . . . . . . . . . . . . . . . . 95 4.19 Breaking the Enigma Code . . . . . . . . . . . . . . . . . . . . . . 123 4.20 The Advanced Encryption Standard . . . . . . . . . . . . . . . . . 132 4.20.1 Final Observation . . . . . . . . . . . . . . . . . . . . . . . 146 8 4.21 GF(2 ) Arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . 149 8 4.22 Polynomials with GF(2 ) Coefficients . . . . . . . . . . . . . . . . 151 4.23 Reed-Solomon Error Correction . . . . . . . . . . . . . . . . . . . 155 4.24 The Queens Problem . . . . . . . . . . . . . . . . . . . . . . . . . 172 4.25 Sudoku . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175 4.26 The Sliding Blocks Puzzle . . . . . . . . . . . . . . . . . . . . . . 183 4.27 The Rubik Cube . . . . . . . . . . . . . . . . . . . . . . . . . . . 203 4.28 Simple series . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241 4.29 e to 2000 decimal places . . . . . . . . . . . . . . . . . . . . . . . 244 2 4.30 The χ test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248 x 4.31 e . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249 √ π 163 4.32 The extraordinary number e . . . . . . . . . . . . . . . . . . 250 4.33 Digits of π . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256 4.34 More commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262 4.35 The VSPL Compiler . . . . . . . . . . . . . . . . . . . . . . . . . 263 4.36 Summary of BCPL . . . . . . . . . . . . . . . . . . . . . . . . . . 264 4.36.1 Comments and GET . . . . . . . . . . . . . . . . . . . . . . 264 4.36.2 Sections . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265 4.36.3 Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . 265 4.36.4 Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . 265 4.36.5 Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . 265 4.36.6 Commands . . . . . . . . . . . . . . . . . . . . . . . . . . 267 4.36.7 Constant expressions . . . . . . . . . . . . . . . . . . . . . 268 4.37 Debugging Techniques . . . . . . . . . . . . . . . . . . . . . . . . 269 4.37.1 Adding debugging output to a program . . . . . . . . . . . 271 4.37.2 Using the interactive debugger . . . . . . . . . . . . . . . . 276 4.37.3 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . 286 5 Interactive Graphics in BCPL using SDL 301 5.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301 5.2 The dragon curve . . . . . . . . . . . . . . . . . . . . . . . . . . . 305 5.3 The Game of life . . . . . . . . . . . . . . . . . . . . . . . . . . . 308 5.4 Collatz Revisited . . . . . . . . . . . . . . . . . . . . . . . . . . . 310 iv CONTENTS 5.5 sdlinfo.b . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 312 5.6 Graphs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315 5.7 Gradients . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318 5.8 Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321 ix 5.9 e and rotation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 325 5.10 The Riemann ζ-function . . . . . . . . . . . . . . . . . . . . . . . 337 5.11 Polar Coordinates . . . . . . . . . . . . . . . . . . . . . . . . . . . 338 5.12 The Mandelbrot Set . . . . . . . . . . . . . . . . . . . . . . . . . 338 5.13 Ball and Bucket Game . . . . . . . . . . . . . . . . . . . . . . . . 350 5.14 The A* Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . 380 5.15 Robots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 402 5.16 Moon Lander . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 444 5.17 A Library for High Prec ision Arithmetic . . . . . . . . . . . . . . 459 5.17.1 A Simple Example . . . . . . . . . . . . . . . . . . . . . . 486 5.18 The Airy Disk . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 492 5.19 A Catadioptric Telescope . . . . . . . . . . . . . . . . . . . . . . . 504 5.20 A 3D Demo using SDL . . . . . . . . . . . . . . . . . . . . . . . . 550 6 Interactive Graphics in BCPL using OpenGL 606 6.1 Introduction to OpenGL . . . . . . . . . . . . . . . . . . . . . . . 607 6.2 Geometric Transformations . . . . . . . . . . . . . . . . . . . . . . 608 6.3 Viewing the Scene . . . . . . . . . . . . . . . . . . . . . . . . . . 611 6.4 A first OpenGL example . . . . . . . . . . . . . . . . . . . . . . . 615 A sdl.h 715 B sdl.b 722 C gl.h 742 D gl.b 747 E gl.b 763 F Package Installation Details 790 F.0.1 Installing BCPL under Linux, the Raspberry Pi and Mac OSX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 790 F.0.2 Installing Emacs under Linux, the Raspberry Pi and Mac OSX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 791 F.0.3 Installing SDL under Linux and the Raspberry Pi . . . . . 792 F.0.4 Installing SDL2 under Linux and the Raspberry Pi . . . . 793 Preface When a new programming language is designed it is invariably strongly in- fluenced by languages that preceded it. One thread of related languages is: Algol -> CPL -> BCPL -> B -> C -> C++ -> Java, indicating that BCPL is just a small link in the chain from the development of Algol in the late 1950s to Java in the 1980s. BCPL is particularly easy to learn and is thus a good choice as a first programming language. It is freely available via my home page (www.cl.cam.ac.uk/~mr10) and the only file to download is called bcpl.tgz. This is easy to decompress and install on the Raspberry Pi and so, in very little time, you can have a usable BCPL system running on your machine. The main topics covered by this document are: • How to connect the Raspberry Pi to a television, keyboard, mouse, and power supply. • How to initialise its SD card with a version of the Linux Operating System. • How to login to the Raspberry Pi followed by a brief description of a few Linux Shell commands. • How to obtain and install BCPL on the Raspberry Pi. • Then follows a series of examples showing how to write, compile and run BCPL programs. • Near the end there are some example programs involving interactive graph- ics using the BCPL interface to the SDL graphics library. • Finally, there is a section outlining some of the debugging aid provided by the BCPL system. Professional computer scientists require a reasonable grounding in mathemat- ics and so some mathematics has been included in this document, but even though some is of university level, the approach taken requires very little mathematical background, and should be understandable by most young people. But if this is not to your taste, skip any sections remotely connected with mathematics. v vi CONTENTS Chapter 1 Setting up the Raspberry Pi The Raspberry Pi is a credit card sized computer that runs the freely available Linux Operating System. I recommend using the Model B version, as shown in Figure 1.1, since it is more powerful and not much more expensive than Model A. It is powered by a typical mobile phone charger using a micro USB connector, but be careful to choose a charger that can supply at least 700 milli-amps. Figure 1.1: Raspberry Pi with connectors The Raspberry Pi can be connected to a TV using an HDMI cable although an analogue connection is also available. With some early versions of Linux for the Raspberry Pi, the HDMI connection failed to work properly. Luckily these early problems seem to have gone away with later versions of the software. 1 2 CHAPTER 1. SETTING UP THE RASPBERRY PI A USB keyboard and mouse is required and a combined wireless keyboard and touch pad is particularly convenient since it allows you to sit in the comfort of an armchair with the keyboard on your knee and the Raspberry Pi neatly hidden behind the TV. The lack of unsightly trailing cables is a clear bonus and leaving the second USB socket free is an added advantage. My favourite keyboard is made by Sandstrøm (available from PC World for about £30). A radio keyboard with a separate mouse might be even better. The picture of the Raspberry Pi shows the tiny USB radio dongle for the keyboard to the left, the HDMI cable above and the micro USB connector for the charger to the right. Figure 1.2 shows the Raspberry Pi fully connected only requiring the HDMI lead to be connected to a TV and the charger plugged into a socket. Notice that at the right side of the machine, you can see part of the blue SD memory card which has to be preloaded with a suitable version of Linux. If you have access to the internet, you can plug a suitable ethernet cable into the Raspberry Pi. This is not absolutely necessary but does have many advantages, particularly for the automatic setting of the date and time, web browsing and downloading software. Figure 1.2: Raspberry Pi and keyboard fully connected The SD card should have a size between 8 and 32 GBytes, although I currently recommend using a card of either 8 or 16 GBytes. I have noticed that some SD cards seem not to work. There are several good web pages supplied by the Raspberry Pi community that describe how to load the Linux image into the SD card. The version of Linux I currently use allows me to login as user pi 1.1. LATER VERSIONS OF THE RASPBERRY PI 3 with password raspberry leaving me connected to a bash shell waiting for Linux commands. Figure 1.3 shows a more extensive setup of the Raspberry Pi. This time it is connected to the internet by cable and has a powered 4-port USB Hub connected to the second USB port. The Hub itself is connected to a 500 Gbyte USB disc drive. The screen shows a typical LXDE desktop with a Midori web browser showing some photos and a terminal session demonstrating the BCPL Cintcode System. Figure 1.3: A more extensive setup 1.1 Later versions of the Raspberry Pi In early February 2015, a new version of the Raspberry Pi became available. It has 1Gb of RAM, 4 USB sockets and is about six times faster the the earlier version. It uses a micro SD card for its disk memory and the machine still costs about same as the previous version. A major advantage is that its operating system provides full support for floating point machine instructions which is invaluable 4 CHAPTER 1. SETTING UP THE RASPBERRY PI since BCPL now supports floating point which is used extensively in programs involving OpenGL graphics. I therefore strongly advise you to upgrade to this version and buy a good quality fast (class 4) micro SD card, typically of size 8Gb. This machine is shown in Figure 1.4. Figure 1.4: The Raspberry Pi Model B-2

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.