Practical Computer Architecture With Python And Arm Alan Clements pdf download
Practical Computer Architecture With Python And Arm Alan Clements pdf download
https://ebookbell.com/product/practical-computer-architecture-
with-python-and-arm-alan-clements-56030964
https://ebookbell.com/product/practical-introduction-to-computer-
architecture-daniel-page-1081154
https://ebookbell.com/product/network-processor-design-
volume-2-issues-and-practices-volume-2-the-morgan-kaufmann-series-in-
computer-architecture-and-design-2nd-edition-mark-a-franklin-1309984
https://ebookbell.com/product/practical-computer-vision-with-simplecv-
the-simple-way-to-make-technology-see-early-release-kurt-
demaagd-2561344
https://ebookbell.com/product/practical-computer-analysis-of-switch-
mode-power-supplies-1st-edition-johnny-c-bennett-2619720
Practical Computer Vision Applications Using Deep Learning With Cnns
With Detailed Examples In Python Using Tensorflow And Kivy 1st Edition
Ahmed Fawzy Gad
https://ebookbell.com/product/practical-computer-vision-applications-
using-deep-learning-with-cnns-with-detailed-examples-in-python-using-
tensorflow-and-kivy-1st-edition-ahmed-fawzy-gad-7280142
https://ebookbell.com/product/practical-computer-vision-applications-
using-deep-learning-with-cnns-with-detailed-examples-in-python-using-
tensorflow-and-kivy-1st-edition-ahmed-fawzy-gad-61258050
https://ebookbell.com/product/practical-computer-literacy-
international-edition-3rd-international-june-jamrich-parsons-11327074
https://ebookbell.com/product/practical-computer-vision-1st-edition-
abhinav-dadhich-61267884
https://ebookbell.com/product/mastering-opencv-with-practical-
computer-vision-projects-daniel-llis-baggio-36252444
BIRMINGHAM—MUMBAI
Every effort has been made in the preparation of this book to ensure the
accuracy of the information presented. However, the information contained
in this book is sold without warranty, either express or implied. Neither the
author, nor Packt Publishing or its dealers and distributors, will be held
liable for any damages caused or alleged to have been caused directly or
indirectly by this book.
Grosvenor House
11 St Paul’s Square
Birmingham
B3 1RB, UK.
ISBN 978-1-83763-667-9
www.packtpub.com
Writing the dedication is the hardest part of writing a book. Of all the
people you have known, who do you honor? I would like to dedicate
this book to four people who have smoothed my path through life, have
been my friends, have set an example, and have made my life worth
living.
To my wife, Sue, who has been my companion and soulmate for over
half a century and has provided so much help with my writing.
To Samuel Gatley, my oldest school friend, who provides me with
inspiration and, like all good friends, is always there to speak to.
To Derek Simpson, who was my Dean at Teesside University for many
years and continues to be a friend. Derek is the most selfless person
I’ve ever met and is always willing to help anyone and everyone.
To Patricia Egerton, a former colleague and friend who had faith in
me when I lacked it and propelled me to make a career decision that
changed the course of my life.
Contributors
Since retiring, Alan has thrown himself into photography and has exhibited
his work several times in the UK, Spain, and Venice.
Preface
Part 1: Using Python to Simulate a
Computer
10
11
12
Index
Most users are no more concerned with computer hardware and architecture
than drivers worry about the operation of their vehicles’ carburetors.
However, a knowledge of computer architecture is useful in many ways,
ranging from how efficiently you operate your computer to maximizing its
security. A good analogy is with pilots. They learn how to fly an aircraft,
and a knowledge of how its engines operate is considered absolutely vital in
handling abnormal situations, prolonging the engine life, and minimizing
fuel consumption.
This book is different. There are books on computer architecture. There are
books on Python. There are books on the Raspberry Pi computer. Here, we
combine all three of these topics. However, I don’t do this in a superficial
way leaving the reader with a shallow and unsatisfactory knowledge of each
topic.
So, how does Python fit into this scheme? Python is a popular high-level
programming language that is freely available for use on the PC, Apple
Mac, and Raspberry Pi. Moreover, Python is probably the easiest computer
language to learn, and it is remarkably powerful.
People learn by doing. I have decided to include sufficient Python for the
reader to construct a simple computer simulator that can read a machine-
level computer instruction and execute it. Because I will show how this
Python simulator works, students can build computers to their own
specifications. They can experiment with instruction sets, addressing
modes, instruction formats, and so on. They can even build different types
of computers to their own specifications, for example, by using complex
instruction set computer (CISC) or reduced instruction set computer
(RISC) architectures. CISC and RISC offer two different philosophies of
computer design. Essentially, RISC computers have fixed-length
instructions that permit only register load and store memory operations,
whereas CISC computers can have variable-length instructions and permit
direct data operations on memory. In reality, the distinction between RISC
and CISC is more complex. The first generation of microprocesses all
conformed to CISC philosophy.
Readers can build computers because they can write a program in Python
that will execute the target language of a specific computer architecture and
they can design that target language themselves.
Students who are taking a computer architecture course and would like
to enhance their experience of computer architecture by experimenting
with their own CPUs by means of simulation. This approach would
increase the depth of their knowledge of computer architecture and
enhance their understanding of the trade-offs that the computer designer
faces.
Chapter 10, A Closer Look at the ARM, examines ARM’s instruction set
in greater detail and provides a foundation for writing programs in
assembly language.
In order to write ARM assembly language programs and debug them, you
need a Raspberry Pi. This is a single-board computer and requires a power
supply, keyboard, mouse, and monitor. I have used both the Raspberry Pi 3
Model A+ and Raspberry Pi 4 Model B versions.
If you are using the digital version of this book, we advise you to type
the code yourself or access the code from the book’s GitHub repository
(a link is available in the next section). Doing so will help you avoid any
potential errors related to the copying and pasting of code.
Conventions used
There are a number of text conventions used throughout this book.
Code in text: Indicates that words in text are not plain English words, but
are words belonging to a program.
The break instruction breaks out of the while loop (that is, execution
continues beyond the end of the loop - it’s a sort of short-circuit
mechanism).
Get in touch
Feedback from our readers is always welcome.
General feedback: If you have questions about any aspect of this book,
email us at customercare@packtpub.com and mention the book title in the
subject of your message.
Errata: Although we have taken every care to ensure the accuracy of our
content, mistakes do happen. If you have found a mistake in this book, we
would be grateful if you would report this to us. Please visit
www.packtpub.com/support/errata and fill in the form.
Piracy: If you come across any illegal copies of our works in any form on
the internet, we would be grateful if you would provide us with the location
address or website name. Please contact us at copyright@packt.com with a
link to the material.
Your review is important to us and the tech community and will help us
make sure we’re delivering excellent quality content.
Do you like to read on the go but are unable to carry your print books
everywhere?
Is your eBook purchase not compatible with the device of your choice?
Don’t worry, now with every Packt book you get a DRM-free PDF version
of that book at no cost.
Read anywhere, any place, on any device. Search, copy, and paste code
from your favorite technical books directly into your application.
The perks don’t stop there, you can get exclusive access to discounts,
newsletters, and great free content in your inbox daily.
https://packt.link/free-ebook/9781837636679
3. That’s it! We’ll send your free PDF and other benefits to your email
directly
Part 1: Using Python to Simulate a
Computer
In this part, you will be introduced to two threads: the digital computer and
the programming language Python. The purpose of this book is to explain
how a computer works by constructing a computer in software. Because we
assume that the reader will not have a knowledge of Python, we will
provide an introduction to Python. However, we will cover only those
topics relevant to building a computer simulator are explored. The topics of
the structure and organization of a computer and Python programming are
interleaved. Once we have introduced theTC1 (Teaching Computer 1)
simulator, the two final chapters will first explore ways of enhancing the
simulator's functionality, and then look at simulators for alternative
architectures.
Once we’ve introduced the concept of digital systems, the next chapter will
demonstrate how a computer operates by fetching instructions from
memory and executing them. After that, we will introduce Python and
demonstrate how you can write a program to simulate a computer and
observe its operations. This book is all about learning by doing; by building
a computer with software, you will learn how it operates and how to extend
and modify it.
The remainder of this book will look at a real computer, a Raspberry Pi, and
show you how to write programs for it and observe their execution. In
doing so, we will move on from simulating a hypothetical computer to
learning about a real computer.
We are going to pose a simple problem and then solve it. Our solution will
lead us to the concepts of algorithms and computers, and also introduce key
concepts such as discrete digital operations, memory and storage, variables,
and conditional operations. By doing this, we can determine the types of
operations a computer needs to perform to solve a problem. After this, we
can ask, “How can we automate this? That is, how can we build a
computer?”
It’s a trite statement, but once you understand a problem, you’re well on the
way to finding a solution. When you first encounter a problem that requires
an algorithmic solution, you have to think about what you want to do, rather
than how you are going to do it. The worst approach to problem solving is
to start writing an algorithm (or even actual computer code) before you
have fully explored the problem. Suppose you were asked to design a cruise
control system for an automobile. In principle, this is a very simple problem
with an equally simple solution:
IF cruise control on THEN keep speed constant
ELSE read the position of the gas pedal
Couldn’t be simpler, could it? Well, what happens if you’ve selected cruise
control and someone pulls out in front of you? You could brake, but this
algorithm would attempt to keep the speed constant while you are braking
by applying full throttle at the same time. Alternatively, you might suggest
that the act of braking should disengage the cruise control mechanism. But
is the cruise control to be disengaged permanently, or should the automobile
accelerate to its previous speed once the braking action has ceased? You
have to think about all the aspects of the problem.
Even if you design a correct algorithm, you have to consider the effect
erroneous or spurious data will have on your system. One of the most
popular criticisms of computers is that they produce meaningless results if
you feed them with incorrect data. This idea is summed up by the
expression garbage in, garbage out (GIGO). A well-constructed
algorithm should detect and filter out any garbage in the input data stream.
In this chapter, we will introduce the following topics:
Technical requirements
You can find the programs used in this chapter on GitHub at
https://github.com/PacktPublishing/Practical-Computer-Architecture-with-
Python-and-ARM/tree/main/Chapter01.
Now, imagine state space, which is a grandiose term for all the states a
system can be in (for example, a plane can be in the climbing, descending,
or level flight state). States are a bit like time, except that you can go
forward or backward between discrete points in state space. If there are a
limited number of possible states, a device that models the transitions
between states is called a finite state machine (FSM). An elevator is a
finite state machine: it has states (position at the floors, doors open or
closed, and so on) and inputs (the elevator call buttons, floor selection
buttons, and door open and close buttons).
Before we take a serious look at FSMs, let’s begin with a simple example of
how to use FSMs to describe a real system. Consider the TV of yesterday,
which is a device with two states: on and off. It is always in one of these
two states, and it can move between these states. It is never in a state that is
neither on nor off. Modern TVs often have three states – on, standby, and
off– where the standby state provides a fast-on mechanism (that is, part of
the electronics is in an active on state, but the display and sound system are
powered down). The standby state is often called the sleep state or idle
state. We can model discrete states using a diagram. Each state is
represented by a labeled circle, as demonstrated in Figure 1.1:
Figure 1.1 – Representing the three states of a television
Figure 1.1 shows the three states, but it doesn’t tell us the most important
information we need to know: how we move between states. We can do this
by drawing lines between states and labeling them with the event that
triggers a change of state. Figure 1.2 does this. Please note that we are
going to construct an incorrect system first to illustrate some of the
concepts concerning FSMs:
Figure 1.2 – Representing the states of a television with transitions
In Figure 1.2, we labeled each transition by the event that triggers it; in
each case, it’s pressing a button on the remote controller. To go from off to
on, you have to first press the standby button and then the on button. To go
between on and standby, you must press the on button or the standby
button.
We’ve forgotten something – what if you are already in a state and you
press the same button? For example, let’s say the TV is on, and you press
the on button. Also, what’s the initial state? Figure 1.3 rectifies these
omissions.
Figure 1.3 has two innovations. There is an arrow to the off state marked
Power on. This line indicates the state the system enters when you first plug
it into the electricity supply. The second innovation in Figure 1.3 is that
each state has a loop back to itself; for example, if you are in the on state
and you press the on button, you remain in that state:
The state diagram shown in Figure 1.3 has both a logical error and an
ergonomic error. What happens if you are in the off state and press the on
button? If you are in the off state, pressing the on button (in this system) is
incorrect because you have to go to standby first. Figure 1.4 corrects this
error by dealing with incorrect inputs:
Figure 1.4 – The TV control with wrong button correction
Figure 1.4 now provides correct operations from any state and includes the
effect of pressing buttons that cause no change of state. But we still have
the ergonomic error – that is, it’s a correct design that behaves in a way that
many would consider poor. The standby state is a convenience that speeds
up operations. However, the user does not need to know about this state – it
should be invisible to the user.
We’ve labored with this example because the notion of FSMs is at the heart
of all digital systems. All digital systems, apart from the most trivial, move
from state to state, depending on the current input and past states. In a
digital computer, the change-of-state trigger is the system clock. A modern
computer operating at a clock speed of 4 GHz changes state every 0.25 x
10-9 s or every 0.25 ns. Light traveling at 300,000 km/s (186,000 mph)
moves about 7.5 cm or 3 inches during a clock cycle.
Traffic lights example
Let’s look at a second example of an FSM. A classic example of an FSM is
traffic lights at a crossroads. Consider an intersection with traffic moving
north-south or east-west. The traffic may move in only one direction at a
time. Assume that this is a system with a clock and a change of state is
permitted every minute:
We can use Table 1.1 to describe this system. We have provided the current
state of the lights (direction of traffic flow), indicated whether any traffic
had been detected in either the north-south or east-west direction, the action
to be taken at the next clock, and the next state. The traffic rule is simple:
the lights remain in their current state unless there is pending traffic in the
other direction.
We can now convert this table into the FSM diagram shown in Figure 1.6.
Note that we have made the east-west state the power on state; this is an
arbitrary choice:
The FSM is also an abstract machine in the sense that it models a real
system, but we don’t have to worry about how the FSM is implemented
in real hardware or software
"Täydellisesti."
"Luotan teidän tunnollisuuteenne." Pitkän vaitiolon jälkeen hän
lisäsi: "Tuo kuningas oli todellakin aivan erikoinen ilmiö — varsin
kaunis ja älykäs henkilö. Eikö teidänkin mielestänne?"
Rivimme olivat yöllä vielä vahvistuneet, sillä luolilta oli tullut uusi
lähetys alkuasukkaita, ja meitä oli nyt noin neljän- tai viidensadan
miehen voimainen joukko samoamassa eteenpäin. Muutamia
tiedustelijoita pantiin kulkemaan rivien edellä ja heidän jäljessään
muu väki taajana armeijana nousi pensaita kasvavaa rinnettä,
kunnes saavuttiin metsän rintaan. Täällä jakaantuivat miehet
keihäänheittäjien ja kaarella-ampujain kahteen ohueen,
kevytliikkeiseen riviin. Roxton ja Challenger sijoittautuivat oikean
siiven päähän Summerleen ja minun ottaessa paikkamme
äärimmäisiksi vasemmalle. Tämä oli kivikauden aikainen sotajoukko
jota me seurasimme taisteluun — me, joilla varustuksina oli St.
James's Streetiltä ostetut pyssynrakennustaiteen viimeiset tuotteet.
ebookbell.com