More results...

Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors
post
page
Python IDE Dashboard

DOS Emulator using Python

In the 1980’s, when IBM introduced the IBM PC which was built with the Intel 8088 microprocessor, they needed an operating system. They approached Microsoft who provided IBM with the “Microsoft Disk Operating System”: MS-DOS.

A Disk Operating System is operated via a command prompt. The user can interact by typing key commands to maintain and operate their computer. One of the key functionality of a DOS operating system is the ability to navigate through the files and folders structure and create, rename, move or delete files and folders.

ms-dos-prompt

Try it yourself, by accessing this online DOS emulator.

In this blog post we will create a “DOS emulator” but only focusing on the navigation and maintenance of the files and folders structure. We will not implement other functionalities of a DOS.

Files & Folders Tree Structure

On a computer system, files and folders are organised using a Tree structure. For instance, let’s consider the following folder structure:
files-folders-structure

In MS-DOS, the main commands to navigate through and maintain a folder structure are:

Command Description
DIR To list the content of a directory/folder.
CD To change directory/folder. This can be used to access a subfolder or a parent folder (e.g. cd ..)
MKDIR To make a new sub-directory/folder.
DEL To delete a file or a directory/folder. This would delete the content (subfolders and files of the directory)
MOV To move a folder or a file from one folder to another.
RENAME To rename a folder or a file.

Class Diagram

We will use a File class and a Folder class to implement this tree structure:
file-folder-classes

Python Code

We have started the code for you and implemented the CD, DIR, MKDIR, DELETE and RENAME instructions as well as a HELP and a CLS (clear screen) instruction. Your task is to complete this code to create the MOVE instruction and apply it to the Folder class to let the user move a file or folder within a subfolder or the parent folder. For instance to move a file to the parent folder, the user would type the following instruction:
MOVE sea.jpg ..
Or to move a file into a subfolder called “landscapes”:
MOVE sea.jpg landscapes

4-bit counter using D-Type flip-flop circuits

In this blog post we will design an electronic circuit using logics gates (combined into D-Type flip-flop circuits) to create a 4-bit binary counter. This approach will help us understand how a program counter may be designed within the CPU and automatically incremented for each tick of the clock cycle.

D-Type Flip-Flop Circuits?


A D-Type Flip-Flop circuit is built using four NAND logic gates connected as follows:
D-Type-Flip-Flop-Logic-Gates

We represent a D-Type Flip-Flop Circuit as follows. You can change the input values D and E by clicking on the corresponding buttons below to see the impact on the outputs Q and Q.




D-Type-Flip-Flop-Circuit



A D-Type Flip-Flop Circuit can be used to store 1 bit of information. It has two input pins (Called D (Data) and E (Enabler) and two output pins (Q and Q = NOT Q).

You can read more about how Random Memory is designed using D-Type flip flop circuits.

The truth table of a D-Type Flip-Flop circuit is as follows:
D-Type-Flip-Flop-Truth-Table

When the enabler input E is set to 1, the output Q can be set to the Data input D.
When the enabler input E is set to 0, the output Q cannot be changed. It remains as its previous value. In other word it retains its value. This is why this circuit is used to create memory cells (e.g in the RAM).

You can test this circuit online by clicking on the picture below:

Frequency Division

Another use of a D-Type flip-flop circuit is to perform a frequency division of a signal. For this type of circuit, we will need to use a slight variation to our D-Type Flip Flop by using an edge triggered D-type flip flop. The difference being that the output Q and Q can only change state with the transition of the clock pulse, which means when the Enabler is changing state from 0 to 1. After that, when the enabler is set to 1, the outputs Q and Q remain the same even when the data input D changes.

You can test this circuit online by clicking on the picture below:

By creating a feedback loop (connecting the output Q to the Data pin, D) and applying a regular clock signal to the Enabler pin (E), the resulting signal (pin Q) has the frequency of the input signal divided by two.
D-Type-Flip-Flops-feedback-loop
This diagram represents both the input and output signals when a feedback loop is applied to a D-Type flip-flop circuit:
D-Type-Flip-Flops-frequency-division
You can test this frequency divider circuit by clicking on the picture below:

Designing a 4-bit binary counter

By applying the same circuit in series we can then divide the frequency by 2, 4 and 8. The original signal (clock) and the 3 resulting signals will then produce the desired counting effect:
4-bit-counter-signals

You can test this circuit with our logic gates circuit simulator by clicking on the picture below:

And here is the full circuit using Logic.ly:
4-bit-counter-d-type-flip-flop

Tagged with:

Solving a Murder Mystery using Prolog

murder-mystery-cluedo
In the middle of last winter, eight guests were invited to a luxurious retreat at the Duke of York Grand Hotel. On the last day of their three-day getaway, the guests were free to vacate to their own occupations. Mrs White and Reverend Green did some “gardening” walking alongside the water fountains, Colonel Mustard and Professor Plum played golf (alone though, purposefully avoiding each other). The other guests spent their days either in their rooms or in the lounge, by the log fire. Later on in the afternoon, all the guests were indoors and Colonel Mustard was seen playing cards with Reverend Green and Mrs Peacock.

As the guests were called for dinner, they soon realised that Dr Black was missing. He was later found lying down on the floor of his bedroom. Dr Black had been shot dead using an old fashion revolver. Except from a few muddy footprints at the entrance of his bedroom, there was no other evidence left by the murderer.

Here is the list of all the guests for the weekend and the rooms they were staying in. Note that the hotel consists of twin bedrooms accommodating two guests per room. We also know that three of the guests (Reverend Green, Colonel Mustard and Madame Rose) own a revolver that they brought with them and kept in their room.

murder-mystery-clues

– Click on the picture to zoom in/enlarge –

Prolog?

Prolog is a language built around the Logical Paradigm: a declarative approach to problem-solving.

There are only three basic constructs in Prolog: facts, rules, and queries.

A collection of facts and rules is called a knowledge base (or a database) and Prolog programming is all about writing knowledge bases. That is, Prolog programs simply are knowledge bases, collections of facts and rules which describe some collection of relationships that we find interesting.

So how do we use a Prolog program? By posing queries. That is, by asking questions about the information stored in the knowledge base. The computer will automatically find the answer (either True or False) to our queries.

Source: http://www.learnprolognow.org/

Knowledge Base (Facts & Rules)

Let’s see how a detective with some good computer science skills would solve this murder mystery. Their first step would be to write all the facts about the case using the Prolog syntax:

/* Facts */
man(dr_black).
man(reverend_green).
man(colonel_mustard).
man(professor_plum).

woman(mrs_peacock).
woman(madame_rose).
woman(miss_scarlett).
woman(mrs_white).

victim(dr_black).

playing_cards(colonel_mustard).
playing_cards(reverend_green).
playing_cards(mrs_peacock).

gardening(mrs_white).
gardening(reverend_green).

played_golf(professor_plum).
played_golf(colonel_mustard).

smoker(miss_scarlett).
smoker(colonel_mustard).
smoker(mrs_white).
smoker(dr_black).
smoker(mrs_peacock).

room(room_21).
room(room_22).
room(room_23).
room(room_24).
room(room_25).

stay_in(dr_black,room_22).
stay_in(reverend_green,room_24).
stay_in(miss_scarlett,room_21).
stay_in(colonel_mustard,room_24).
stay_in(professor_plum,room_22).
stay_in(mrs_peacock,room_23).
stay_in(madame_rose,room_21).
stay_in(mrs_white,room_23).

owns_revolver(reverend_green).
owns_revolver(colonel_mustard).
owns_revolver(madame_rose).

Then our detective would write some rules and apply them to these facts.

A suspect is either a man or a woman who is not the victim!

#Rule #1:

This is how this rule would be implemented in Prolog:

suspect(X):- man(X), \+victim(X).
suspect(X):- woman(X), \+victim(X).

Any suspect who was seen playing cards at the time of the crime has a valid alibi!

#Rule #2:

This is how this rule would be implemented in Prolog:

has_alibi(X):- suspect(X), playing_cards(X).

Anyone who did some gardening, played golf or is a smoker has been outside at least once throughout the day.

#Rule #3:

This is how this rule would be implemented in Prolog:

went_outside(X):- gardening(X).
went_outside(X):- smoker(X).
went_outside(X):- played_golf(X).

The rooms of the hotel are twin rooms that can accomomdate two guests. Two guests share the same room if they both are booked in the same room.

#Rule #4:

This is how this rule would be implemented in Prolog:

share_room(X,Y):- room(R), stay_in(X,R), stay_in(Y,R), X \= Y.

Anyone who owns a revolver or share a room with another guest who owns a revolver, had access to a revolver!

#Rule #5:

This is how this rule would be implemented in Prolog:

revolver_access(X):- owns_revolver(X).
revolver_access(X):- share_room(X,Y), owns_revolver(Y).

Solving this murder case!

Step 1: Can you use all the above facts & rules to work out the actual murderer?

Step 2: Can you write one final rule needed to solve this murder mystery in just one query?

To find our culprit we need to find a suspect, who went outside (we found some muddy footprints on the floor next to Dr Black’s body), who has no alibi and who had access to a revolver!

You can access the detective’s knowledge base:
https://swish.swi-prolog.org/p/murder-mystery.pl
Murder Mystery CodeOpen in New Window

Solution:

This is how our final rule would be implemented in Prolog:
View Solution!

Going a step further…

Now that you have solved this task, update the case by adding additional characters/suspects and additional clues. Make sure that at the end, the murderer of your case can still be found in just one query.

As additional suspects you could add some additional guests:

  • Mr Peach staying in room 25
  • Dr Orchid staying in room 25

Decide whether they went outside or not, whether they own a revolver or not, whether they were seen at the table playing card games or not! You can also add new indoors or outdoors activities.

You may also consider adding hotel staff members e.g.:

  • A barman,
  • A receptionist.

Staff members would have access to all the rooms!

Tagged with:

OOP Programming: Classes & Objects

OOP-Class-ObjectIn this blog post we will investigate how to create some basic classes and objects that could be used in a range of programming projects.

Classes and Objects are the building blocks of Object-Oriented programs. You can find out about more about the key concepts of Object-Oriented Programming (OOP).

A class is a template or blueprint from which objects will be instantiated. It’s like the mould from which objects can be created/instantiated from. A class represents a real life concept such as a person, an animal, a car, a house or a football.

A class consists of a collection of states (a.k.a. attributes or properties) and behaviours (a.k.a. methods).

An object is an instance of a class. The attributes of an object as stored using variables for storing data and the behaviour/methods of an object are implemented as functions or procedures and can be used to perform operations on the data.

Most classes have one method called a constructor. It is a function that is automatically called when an object is created/instantiated from a class. It is mainly used to initialise the default values for each attribute/variable of the object. In Python the constructor of a class is the __init__() function.

Dice ClassPadlock ClassDeck of cardsCoin Class
Our first class will be used to create basic game of dice. The Dice class will have two properties: numberOfSides and value and one method throw() that will update the dice value by generating a random number between 1 and numberOfSides (e.g. between 1 and 6 for a standard 6-side dice).
Dice-Class

Our second class is used to create a padlock with a four digit key. The Padlock class has an unlock() method used to test a combination to try to unlock the padlock.

The key of the padlock is stored in a private property: __key (In Python the “__” prefix is used to indicate a private property or method.) The key can still be accessed from outside the class using the two public methods getKey() and setKey(). This is an example of encapsulation.
Padlock-Class

To create a deck of cards we will create two classes as follows:
Card-and-Deck-Class

In order to create a game of Head or Tail we will create a Coin Class using two properties: faceUp (which will be either Head or Tail) and value of the coin (in pence) and one method to flip the coin.
coin-class
We will then create a game with ten rounds and two players. For each round, a coin will be created using a random value (1p, 2p, 5p, 10p, 20p, 50p, £1 or 2£). After flipping the coin, if the visible face is “Head”, player 1 wins the value of the coin otherwise it’s given to player 2.

Programming Challenge

Use the two classes Card and Deck (see above) to implement the card game “War” between two players using the following rules:

  • The deck of card is evenly divided among two players.
  • Battle: In unison, both players reveals the top card of their deck and the player with the higher card takes both of the cards played and add them to the bottom of their deck.
  • Rank: Aces are high, and suits are ignored.
  • War: A war occurs when the two cards played are of equal value. Both players place the next card of their pile face down and then another card face-up. The owner of the higher face-up card wins the war and wins all the cards on the table. If the face-up cards are once again of equal value then the battle repeats with another set of face-down/up cards. This repeats until one player’s face-up card is higher than their opponent’s.
  • Game Over: The objective of the game is to win all of the cards. The first player to run out of cards in their deck looses the war!
Tagged with: ,

Bubble Sort Algorithm in LMC

The aim of this challenge is to implement a Bubble sort algorithm on a list of 10 values using LMC.
We will be using our online LMC simulator to test our code
online LMC simulator:
LMC SimulatorOpen in New Window

LMC code using hardcoded valuesLMC code using input values
To simplify the testing process (and nto having to input 10 values each time we want to run/test the code, we have created a version of our Bubble sort algorithm where the 10 values are hardcoded in the code itself. You can change these values (v1 to v10) in the code from line 70 to 79.

init   LDA v0
       STA 90
       LDA v1
       STA 91
       LDA v2
       STA 92
       LDA v3
       STA 93
       LDA v4
       STA 94
       LDA v5
       STA 95
       LDA v6
       STA 96
       LDA v7
       STA 97
       LDA v8
       STA 98
       LDA v9
       STA 99

loop   LDA true
       STA sorted
       LDA first
       STA pos
       ADD one
       STA next
step   LDA @pos
       SUB @next
       BRZ pass
       BRP swap

pass   LDA pos
       ADD one
       STA pos
       LDA next
       ADD one
       STA next
       LDA pos
       SUB last
       BRZ repeat
       BRA step
   
swap   LDA @next
       STA temp
       LDA @pos
       STA @next
       LDA temp
       STA @pos
       LDA false
       STA sorted
       BRA pass

repeat LDA sorted
       SUB true
       BRZ exit
       BRA loop

exit   OUT
       HLT

pos    DAT
next   DAT 
temp   DAT
sorted DAT 0
true   DAT 1
false  DAT 0
one    DAT 1
first  DAT 90
last   DAT 99
v0     DAT 32
v1     DAT 7
v2     DAT 19
v3     DAT 75
v4     DAT 21
v5     DAT 14    
v6     DAT 95    
v7     DAT 35    
v8     DAT 61    
v9     DAT 50   
This version of the code will start by asking the user to enter 10 different numerical values that the code will then sort.

init   INP
       STA 90
       INP
       STA 91
       INP
       STA 92
       INP
       STA 93
       INP
       STA 94
       INP
       STA 95
       INP
       STA 96
       INP
       STA 97
       INP
       STA 98
       INP
       STA 99

loop   LDA true
       STA sorted
       LDA first
       STA pos
       ADD one
       STA next
step   LDA @pos
       SUB @next
       BRZ pass
       BRP swap

pass   LDA pos
       ADD one
       STA pos
       LDA next
       ADD one
       STA next
       LDA pos
       SUB last
       BRZ repeat
       BRA step
   
swap   LDA @next
       STA temp
       LDA @pos
       STA @next
       LDA temp
       STA @pos
       LDA false
       STA sorted
       BRA pass

repeat LDA sorted
       SUB true
       BRZ exit
       BRA loop

exit   OUT
       HLT

pos    DAT
next   DAT 
temp   DAT
sorted DAT 0
true   DAT 1
false  DAT 0
one    DAT 1
first  DAT 90
last   DAT 99

Executing the Code

We will be using our online LMC simulator to test our code
online LMC simulator:
LMC SimulatorOpen in New Window

Before testing this code in the online LMC simulator, we recommend increasing the clock speed:
LMC-clock-speed-max
We also recommend turning off the log file:
LMC-log-file-off
This will significantly reduce the execution time. With the data given, it will still take 831 FDE cycles to sort all 10 numbers:
LMC-number-of-FDE-cycles
LMC-sorted-list

Code Review

Let’s review how this code was constructed:

InitialisationBubble Sort
When running this code using the LMC simulator, you will notice that the first action performed by this code is to store the 10 given values in the RAM using locations 90 to 99:

bubble-sort-lmc-memory

This is done at the very start of the code in the init block:

init   LDA v0
       STA 90
       LDA v1
       STA 91
       LDA v2
       STA 92
       LDA v3
       STA 93
       LDA v4
       STA 94
       LDA v5
       STA 95
       LDA v6
       STA 96
       LDA v7
       STA 97
       LDA v8
       STA 98
       LDA v9
       STA 99

This code is using the 10 labels v1 to v9 declared at the end of the code from line 70:

v0     DAT 32
v1     DAT 7
v2     DAT 19
v3     DAT 75
v4     DAT 21
v5     DAT 14    
v6     DAT 95    
v7     DAT 35    
v8     DAT 61    
v9     DAT 50   
For each iteration of the Bubble sort algorithm we set a “sorted” flag to true.

loop   LDA true
       STA sorted

This flag will be overwritten if when scanning through the list a swap is made. If after checking all values of the list, the “sorted” flag is still equal to true, in this case we can deduct that the list if fully sorted and hence stop iterating.


We then reset the two pointers “pos” and “next” to first two memory locations of the list (memory location 90 (first) and 91 (first ADD one).

       LDA first
       STA pos
       ADD one
       STA next

We can now compare the two adjacent values at position “pos” and “next” (=pos+1). Note the use of indirect addressing using the @ symbol to access the values stored at the address stored at position “pos” and “next”. To compare both values we perform a SUB (subtract) operation.
If the current value (@pos) is greater then the next value (@next) then the subtraction will be positive. If both adjacent values are equal then the subtraction will be null and there is no need to swap both values (BRZ pass). If not, if the subtraction is a positive number (>0) then we will swap both values: BRP swap. If not we will move on the next section of code to pass without swapping.

step   LDA @pos
       SUB @next
       BRZ pass
       BRP swap

To pass, we increment both pointers “pos” and “next” by 1 so that we can then repeat the process with the next two values (by branching to the “step” block of code). However, we also need to check if we have reached the end of the list (if the “next” pointer is pointing to the last value in the list. In this case we would need to check if we need to start a new iteration of the Bubble sort algorithm from the very start of the list, or whether we can stop here (if the list is already fully sorted). This check will be performed in the “repeat” block of code.

pass   LDA pos
       ADD one
       STA pos
       LDA next
       ADD one
       STA next
       LDA pos
       SUB last
       BRZ repeat
       BRA step

Below is the code used to swap both adjacent values (at position “@pos” and “@next”). This also sets the “sorted” flag to false, to indicate that the list may not be fully sorted yet and that a new iteration of the Bubble Sort will be needed.

swap   LDA @next
       STA temp
       LDA @pos
       STA @next
       LDA temp
       STA @pos
       LDA false
       STA sorted
       BRA pass

The “repeat” block of code is used to decide whether the list is fully sorted or whether a new full iteration of the list is needed to continue the sorting process. This is done by checking that the flag “sorted” is true or false. If it is true, then the list is sorted and we can stop the Bubble Sort algorithm by branching to the “exit” block of code. Otherwise we are going back to the star of the process by branching to the “loop” block of code.

repeat LDA sorted
       SUB true
       BRZ exit
       BRA loop

Finally, the “exit” block of code is used to inform the user that the list is now fully sorted. This is done by outputting the value 0 and stopping the process (HLT instruction).

exit   OUT
       HLT
Tagged with:

Memory Filler in LMC

The aim of this challenge is to write a program in LMC that will fill in the memory/RAM with the value 999, from memory location 50 onwards (up to memory location 99).

Note that even though such a program may seem pointless, a memory/RAM filler can serve a purpose in some contexts e.g.:

  • A memory filler program can be used for testing purposes, for instance to investigate how a system behaves when the available RAM memory is running low or reaching its full capacity. (Will the system crash? will the system make use of virtual memory?)
  • A memory filler program can be used for benchmarking purposes to evaluate/compare memory writes efficiency/speed.

First Attempt

So let’s start the code with a very basic program to fill up the next 5 memory locations:

       LDA value
       STA 50
       STA 51
       STA 52
       STA 53
       STA 54
       HLT
value  DAT 999

You can try this code using the online LMC simulator:
LMC SimulatorOpen in New Window

Using a loop?

The above code would be unsustainable if we planned to over-write million of memory locations. It would seem that using a loop/branching instruction would be recommended here.

The idea would be to use a label to store the memory address to write on. We would initialise this label to 50 and increment this value by 1 for each iteration of the code:

start   LDA value
        STA ???
        LDA address
        ADD one
        STA address
        BRA start
        HLT
value   DAT 999
address DAT 50
one     DAT 1

>

The above code shows how the loop can be implemented. However there is an issue with line 1 of this code. The aim is to store the value currently held in the accumulator at the memory location stored in memory location “address”.
This cannot be achieved with direct addressing!

Solution 1: Using Indirect Addressing

For the above code to work, we need to rely on indirect addressing.
With the online LMC simulator, this can be achieved using the @ symbol.
To find out more about memory address modes, you can read the following blog post.

start   LDA value
        STA @address
        LDA address
        ADD one
        STA address
        BRA start
        HLT
value   DAT 999
address DAT 50
one     DAT 1

You can try this code using the online LMC simulator:
LMC SimulatorOpen in New Window

Solution 2: Using Self-Modifying code

Not all processors support indirect addressing. (Especially in the early days of “computing history”). In this case an alternative approach consists of using self-modifying code that can be implemented without the need to use indirect addressing.

You can investigate this approach further by reading through this challenge/post.

Tagged with:

Self-modifying code in LMC

In computer science, self-modifying code is code that alters its own instructions while it is executing. Getting code that overwrites itself can seem dubious, rather risky and hence unrecommended. However in some cases, it is a technique that can be used to significantly improve the performance of a piece of code or to achieve a behaviour from a program that would be more complex to implement, requiring significantly more code to achieve the same output.

Self-modifying code is fairly straight forward to achieve with a Low-Level language when using a Von Neumann computer architecture where both data and instructions are stored in the same memory.

You may even already have inadvertently created your own low-level code (e.g. using LMC) where your program was over-riding itself (by mistake more than by design) when storing data in a memory cell already used to store an instruction of your program.

For instance, let’s consider the following program written using LMC:

  1. INP
  2. STA 3
  3. HLT

You can try this code using the online LMC simulator:
LMC SimulatorOpen in New Window

If you load this code into your RAM using the LMC simulator, it will use the first 3 memory locations (a.k.a mailboxes 00, 01 & 02) of the RAM to store the 3 instructions of this program:
LMC-Code-Memory

As you execute this program, the user input would be stored in the fourth memory location of the RAM (mailbox 03). E.g. This because the LMC simulator uses direct addressing where the operand of an instruction is the memory location of where the data is to be loaded from or, for an STA instruction, where the data will be stored. For instance, with our code, if the user inputs the value 999:
LMC-Input-Memory
As you can see, when using the LMC simulator, both instructions and program data are stored in the same memory using the same format. This is one of the key characteristics of a Von Neumann computer architecture that the LMC simulator is emulating.

Now let’s consider our initial program and add some extra lines of code, for instance to ask for a second user input and to store the second input value next to the first input value. It would be tempting to write the following code:

  1. INP
  2. STA 3
  3. INP
  4. STA 4
  5. HLT

LMC-Code-2-Memory

However this code will not work as per our initial intention. Effectively while executing this code, the first STA instruction (STA 3) of this code would save program data (in this case the value typed in by the user) over an existing instruction of the program. This is a typical example of self-modifying code that would impact on the expected execution of the program. e.g. You can see what the code in memory would look like if the user entered 999 and 998 as input values:
LMC-Input-2-Memory

A quick solution would be to store both input values in different memory locations, not already used by the program: e.g.

  1. INP
  2. STA 8
  3. INP
  4. STA 9
  5. HLT

LMC-Input-3-Memory

However this is not the recommended solution. Effectively, there is no guarantee that when this program is executed, memory locations 8 and 9 are not already being used by another program.

The recommended solution is to use labels in your code. When the code is loaded in memory (assembled), the labels are automatically replaced with memory locations of unused memory cells.
e.g.

      INP
      STA num1
      INP
      STA num2
      HLT
num1  DAT
num2  DAT

LMC-Labels-Memory
This code uses two labels, num1 and num2.

Self-Modifying Code Example


As explained before, on occasions we can decide to create self-modifying code on purpose as demonstrated in the following example.

The aim of this code is to fill in the RAM with the value 999, from memory location 10 onwards. (e.g. Note that this code would break after reaching the maximum RAM capacity: memory location 99).

Here is the code of our “RAM filler” program. Can you see how we have used self-modifying code to complete this “RAM Filler” program?

start LDA line
      ADD one
      STA line
      LDA data
line  STA 10
      BRA start 
      HLT
data  DAT 999
one   DAT 1

Test this code online:
LMC SimulatorOpen in New Window

Trying to achieve this “memory filling” behaviour without using self-modifying code would be a lot more complex to implement in LMC. You can see how it could be achieved using an alternative approach relying on using indirect addressing by completing this LMC challenge.

Tagged with:

Drone Display

drone-light-showDrone displays/light shows are performed by synchronized and choreographed collections of drones that position themselves to recreate various aerial formations. Each drone is equipped with LED lights that can change colour during the display. The first drone display was presented in 2012 in Austria, where Ars Electronica Futurelab introduced SPAXELS (short for “space elements”) for the first time. More recent drone display includes the Edinburgh’s Hogmanay 2020 New Year Celebrations and the London 2020 New Year Firework and Lights display.

For this challenge, we are using Glowscript to recreate a drone display choreography.

Glowscript/vPython?…


To complete this challenge and find out about all the 3D shapes you can use in Glowscript, you will need to refer to the Glowscript/vPython online documentation.

Drone Display Preview


Here is our drone display demonstration which consists of 3 scenes. Each scene involves 20 drones.

(x,y,z) Coordinates


The drones are positioned in the night sky using (x,y,z) 3D coordinates.

Each scene is defined as a list of 20 sets of (x,y,z) coordinates, to store the position of each drone in the scene. For instance the Moon scene is stored as follows:

#Scene 1: Moon
scene = [(20,180,0),
(-10,160,0),
(-30,130,0),
(-50,100,0),
(-60,60,0),
(-70,20,0),
(-70,-20,0),
(-60,-50,0),
(-40,-80,0),
(-10,-110,0),
(20,-150,0),
(-30,-130,0),
(-70,-110,0),
(-100,-80,0),
(-130,-50,0),
(-140,0,0),
(-130,50,0),
(-110,100,0),
(-80,140,0),
(-40,170,0)]

You can easily recreate your own scene using this online plotting tool:
drone-light-show-plotting-coordinates

Linear Interpolation Formulas


Linear interpolation formulas are used on the (x,y,z) coordinates to reposition the drones from one scene to another creating a smooth animation effect.

Here is an explanation of how linear interpolation formulas can be applied to progressively move a drone from one position (from scene A) to another position (from scene Z), to simplify we are only focusing on 2D coordinates (x,y).
tweening-linear-interpolation

Using the following linear interpolation formulas we can calculate the (x,y) coordinates of each drone position for any “in-between” frame.

x(t) = xA + (xZ – xA) * t / 10
y(t) = yA + (yZ – yA) * t / 10

In 3D, the same formula would be applied to the z coordinate:

z(t) = zA + (zZ – zA) * t / 10
  • “t” represents the time: in other words the frame number (e.g. from 0 to 10 frames)
  • (xA,yA,zA) the coordinates of a drone from the starting position (e.g. scene “A”)
  • (xZ,yZ,zZ) the coordinates of a drone from the ending position (e.g. scene “Z”)

In our animation we have used 100 frames between each scene to create a smooth transition.

RGB Colour Codes and Linear Interpolation Formulas


The same linear interpolation formulas are used on the RGB codes of each drone to smoothly change the drone’s light colours using a gradient animation.

In Glowscript a colour is represented using an (r,g,b) vector where r, g and b are three decimal values between 0 and 1.

The linear interpolation formulas on an RGB colour code are as follows:

R(t) = RA + (RZ – RA) * t / 10
G(t) = GA + (GZ – GA) * t / 10
B(t) = BA + (BZ – BA) * t / 10
  • “t” represents the time: in other words the frame number (e.g. from 0 to 10 frames)
  • (RA,GA,BA) the RGB colour code of a drone on the starting scene (e.g. scene “A”)
  • (RZ,GZ,BZ) the RGB colour code of a drone on the next scene (e.g. scene “Z”)

Camera Position


To improve the preview of our demo, we are also rotating the camera (changing the camera angle) between each frame, to preview the light show using a 365 rotation.

You can also control the position of the camera by right clicking on the canvas and you can zoom-in/out using a wheel mouse.

Tagged with:

Wired & Wireless Connection Methods

Take the Quiz! (open full screen)


Can you recognise the different types of cables used in IT? Do you know what are the main wireless technologies used to connect IT equipment? Take the quiz to check your knowledge of wired and wireless connection methods!

LED Dice Logic Gates Diagrams

LED Dice


Our aim is to create an LED Dice using a breadboard and 7 LEDs disposed as follows:
LED-Dice

We will then use three buttons/switches to control the 7 LEDs of the dice to recreate the following patterns:
LED-Dice-6

Octal Number System

The octal numeral system, or oct for short, is the base-8 number system. It uses 8 digits from 0 to 7. Octal numerals can be converted into binary using 3 binary digits and the following conversion table.
octal-conversion-table

We will use three input buttons A,B,C representing the 3 binary digits to generate 8 binary patterns representing the 8 octal digits from 0 to 7.
LED-Dice-input-output

We will then use logic gates circuits to control each of the 7 LED based on the three inputs:

LED Dice: Truth Tables & Karnaugh Maps


We will use three inputs A,B and C to represent the three digits as ABC (A is the most significant digit, C is the least significant digit). When creating the electronic circuit we will use 3 switches to represent these 3 inputs.

We will need 7 outputs one for each LED. So let’s investigate each LED one at a time.

LED 1 & 6LED 2 & 5LED 3 & 4LED 7
LED 1 & LED 6 (top-left and bottom right) should be on for the following values:

LED-Dice-value-4ABC:100 LED-Dice-value-5ABC:101 LED-Dice-value-6ABC:110 LED-Dice-value-7ABC:111

LED 1 & 6 should be off for the following values:

LED-Dice-value-0ABC:000 LED-Dice-value-1ABC:001 LED-Dice-value-2ABC:010 LED-Dice-value-3ABC:011

Hence the Truth Table for LED 1 & LED 6 is as follows:

Inputs Output
A B C LED 1
0 0 0 0
0 0 1 0
0 1 0 0
0 1 1 0
1 0 0 1
1 0 1 1
1 1 0 1
1 1 1 1

This Truth table can be represented using a Karnaugh Map:

Karnaugh Map for LED 1 & LED 6

Karnaugh Map for LED 1 & LED 6

LED 2 & LED 5 (top-right and bottom left LED) should be on for the following values:

LED-Dice-value-2ABC:010 LED-Dice-value-3ABC:011 LED-Dice-value-4ABC:100 LED-Dice-value-5ABC:101 LED-Dice-value-6ABC:110 LED-Dice-value-7ABC:111

LED 2 & LED 5 should be off for the following values:

LED-Dice-value-0ABC:000 LED-Dice-value-1ABC:001        

Hence the Truth Table for LED 2 & LED 5 is as follows:

Inputs Output
A B C LED 2
0 0 0 0
0 0 1 0
0 1 0 1
0 1 1 1
1 0 0 1
1 0 1 1
1 1 0 1
1 1 1 1

This Truth table can be represented using a Karnaugh Map:

Karnaugh Map for LED 2 and LED 5

Karnaugh Map for LED 2 and LED 5

Follow the same process to define the Truth Table and Karnaugh Map of LED 3 and LED 4 which have the same truth table.

LED 3 & LED 4 (middle-left and middle-right) should be on for the following values:

LED-Dice-value-6ABC:110 LED-Dice-value-7ABC:111        

LED 3 & 4 should be off for the following values:

LED-Dice-value-1ABC:001 LED-Dice-value-2ABC:010 LED-Dice-value-3ABC:011 LED-Dice-value-4ABC:100 LED-Dice-value-5ABC:101 LED-Dice-value-0ABC:000


Follow the same process to define the Truth Table and Karnaugh Map of LED 7 (LED in the middle of the dice).
LED 7 (in the middle) should be on for the following values:

LED-Dice-value-1ABC:001 LED-Dice-value-3ABC:011 LED-Dice-value-5ABC:101 LED-Dice-value-7ABC:111

LED 7 should be off for the following values:

LED-Dice-value-4ABC:100 LED-Dice-value-0ABC:000 LED-Dice-value-2ABC:010 LED-Dice-value-6ABC:110


LED Dice: Boolean Expressions


The Karnaugh maps will help us define the Boolean Expressions associated with each of the 7 LEDs.

LED 1 & 6LED 2 & 5LED 3 & 4LED 7
Karnaugh Map:
Karnaugh Map for LED 1 & 6

Karnaugh Map for LED 1 & LED 6

Boolean Expression:
Boolean Expression for LED1 & LED 6

Boolean Expression for LED1 & LED 6

Karnaugh Map:
Karnaugh Map for LED 2 & LED 5

Karnaugh Map for LED 2 & LED 5

Boolean Expression:
Boolean expression for LED 2 & LED 5

Boolean expression for LED 2 & LED 5

Use the Karnaugh Map for LED 3 & 4 to define the Boolean Expression of LED 3 & LED 4.
Use the Karnaugh Map for LED 7 to define the Boolean Expression of LED 7.

LED Dice: Logic Gates Diagrams


We can now convert each Boolean expression into a Logic Gates circuit to link our 3 inputs (switches) to our 7 LEDs display using a range of logic gates.
LED 1 & LED 6LED 2 & LED 5LED 3 & LED 4LED 7
Boolean Expression:
Boolean Expression for LED1 & LED 6

Boolean Expression for LED1 & LED 6

Logic Gates Diagram:
In this case, the Boolean expression being so basic, there is no need for any logic gates to control LED 1. The LED is directly connected to input A.
Logic Gates Diagram for LED 1 & LED 6

Logic Gates Diagram for LED 1 & LED 6

Boolean Expression:
Boolean expression for LED 2 & LED 5

Boolean expression for LED 2 & LED 5

Logic Gates Diagram:
In this case, the Boolean expression being so basic, only one OR gate is needed using input A and input B.
Logic Gates Diagrams for LED 2 & LED 5

Logic Gates Diagrams for LED 2 & LED 5

Use the Boolean Expression of LED 3 & LED 4 to draw the logic gates diagram required to control LED 3 & LED 4.
Use the Boolean Expression of LED 7 to draw the logic gates diagram required to control LED 7.

Testing


You can now recreate your logic gates circuit using our logic gates circuit simulator to test if it behaves as expected for all 8 entries.

You can also recreate the electronic circuit using bread boards, LEDs, resistors and logic gates or create your electronic cricuit online using tinkercad.

LED Dice - Electronic Circuit - Using AND and OR gates.

LED Dice – Electronic Circuit – Using AND and OR gates.

unlock-access

Solution...

The solution for this challenge is available to full members!
Find out how to become a member:
➤ Members' Area