For excercises

TIK TOK CHINA exercise.

Create a JS program that gets a number from the user and then print the numbers from 0 to the number provided by the user and do the following calcuations.

1- if the number is divisible by 3 it should print TIK instead of the number.

2- If the number is divisible by 5 it should print TOK instead of the number.

3- If the number is divisible by 3 and 5 it should print CHINA instead of the number.

4- Use prompt to get the value from the user.

5- Use console.log to print the output to the console.

Examples of when user inputs 20


2 cristmas tree

Write a program that receives a value from a user using prompt and draw a christmas tree using * and spaces with the same level of lines as the number inserted by the user.

Example when user inputs 5


3 Enigma machine

Create a program that can encrypt messages using the following rules

1-The program should ask if it needs to encrypt or decrypt

2- the program will receive a phrase in english in lower case

3. the program should replace the letters as follows table, (depending if is encrypting or decrypting.

4 any other character should remain unchanged.

q — aw — be — cr — dt — ey — f
u — g i — ho — ip — ja — ks — l
d — mf — ng — oh — pj — qk — r
l — sz — tx — uc — vv — wb — x
n — ym — z

example, if the program receives the string “cat” it should return “vke“. when encrypting

In the same way, if the program gets the string ‘vke’, it should return ‘cat’ if decrypting


if you have a variable string

var str = “hello”

you can get characters at specific part of the string with


you can get the length of the string with


4 Roman Nightmare

Make a program that translates a number provided by the user between 1 and 399 and translates it to roman numbers.

Example when inserting 7 output should be VII

Example when inserting 399 output should be CCCXCIX

Example when inserting 159 output should be CLIX

Notes:, in roman numbers the following equivalences hold true

I = 1

V = 5

X = 10

L = 50

C = 100

III = 3

XXX = 30

III = 300

for loop basics

A for loop allows to execute a piece of code several times.

Example 1.0

for(var i =0; i<10; i++)
    console.log("I am printing this message 10 times");

If the previous code where to run in chrome we could see the following output in the console.

Basic structure of for loop

for(    INITIALIZATION     ;     CONDITION     ;    INCREMENT    )

We have 4 main parts that build the for

  • INITIALIZATION: Executed only one time at all.
  • CONDITION: Executed before the code is executed. (executed several times)
  • CODE: Executed only if CONDITION is meet (executed several times)
  • INCREMENT: Executed after the code is executed. (executed several times)

The following diagram shows how the flow of a for is performed.

so in our original example 1.0 we could spread the parts of the for as follows.

for(var i =0; i<10; i++)
    console.log("I am printing this message 10 times");


  • INITIALIZATION executes only once.
  • the CONDITION will be executed as long as it is meet.
  • Every time the CONDITION is meet, the CODE and the INCREMENT will execute once.

Divoom Tivoo IC components (part 1)

Here we will discuss the electronic components that compose the circuit board of the Tivoo, in this first part we will just find what the components are and what they do.

Lets start with the screen

Tivoo screen back

Here we can see three main IC of the board

ICN2053 – 16-channel PWM


Here you can see a link to this IC



Here you can see a link to this IC×32:icn2012_datasheet.pdf

This is basically a merge between TTL 74HC138 and 8 powermosftets.

F130G4 0 Microcontroller


Here you can see a link to what I think this IC is.

This IC is aparently from a company called GigaDevice, the IC have diferent naming, so may be a clone, Seems this IC was originally a GD32F130G4 from GigaDevice, the Die match at least in the shape. Also the logo looks like GigaDevice, I may be wrong tho.

Now lets jump to the Main board.

main board front Tivoo
main board front 2 Tivoo

Main board back Tivoo

This board have 5 major IC

XB8608A 8u1 – Lithium Battery Protection IC


Here you can see a link of what I think is this IC

This IC seems to be a Once Cell Lithium-ion Protection circuit. The original IC seems to have been XB8686E from Sysemi I presume is similar to that link since the numbers kind of match and the way it is installed relates to the pdf. you can see the top 4 legs are unused, or in this case, maybe connected to the negative part of the battery altogether.

25Q127CSIG – Flash memory


Here you can see a link to what I think is this IC

This IC seems to be Flash memory, the original IC seems to be GD25Q127CSIG

CS8326S B144 – Audio Amplifier


Here you can see a link of what I think this IC is

FP8102 SAe-00L Li-Ion Battery Charger


Here you can see a link to this IC

ANYKA AK1052DN048 GZSH04I18 Microcontroler/DSP


Here you can see a link to this IC

We will discuss the function of each of this ics in future post.


This Post will show my disassembly process as well as screenshot of main components and tips and tricks.

The main structure of the Tivoo is not glued, it consist of some snap locks and screws.

The first step is to remove the knobs, there are two knobs in the unit, those you just need to pull with a bit of force and they will slide of, they are held in place only by pressure and friction so very safe to remove.

Second step is to remove the plastic faceplate. This is the trickiest part, as the face plate is attached with latches.

In the next picture I highlighted the latches.

Tivoo Latches

To start disassembling the device, I started from the knob corner moving up using a guittar pick.

Using guitar pick to remove faceplate.

Once the faceplate is removed, the tivoo looks like this.

Tivoo without faceplate

After that the tivo screen is composed of 4 components.

*Diffuser panel
*ligth mask
*led array

The following images is the device after removing the Glass

Tivoo without Glass

The following image is the device after removing the diffuser

Tivoo without deffuser

The following image is the device without the grid mask.

Tivoo without grid mask.

Here are the images of all the 4 components of the screen.

Tivoo glass, diffuser, mask and led display.

Here is other shot of the LED display turned off.

Tivoo LED display

To continue the disassembly process, we need to remove some 6 screws pointed in the next screenshot.

from this point the screen is just held in place by friction against some panne like material. to extract the screen I used one screw driver in one of the holes and pulled as follows.

removing led screen from Tivoo

After that you can just unplug the two wires, the big one is power and data, the small one is the microphone.

screen connected to Tivoo

Here is another picture of the main board of the screen. (The following picture is highRes so you can see the components.

In the case you can see the main battery (a lithium 18650 w 3000mah), the speaker and another board behind the battery (hard to see). You will also see other two screws that need to be removed to continue with the disassembly.

Screws inside assembly.

After that you can remove the inner assembly from the outer case.

Tivoo inner assembly and outer case.

Now to open the inner assembly you need to remove itehr 4 screws.

Inner assembly Tivoo screws

After that you will need to remove other three screws that attach the circuit boar to the assembly and unglue the battery.

To Remove the battery I just gently moved until the glue broke. Also note that the board itself have some of the same glue, so you will need to pull it gently until the glue gives up after removing the 3 screws from the picture.

Tivoo main board srews

Here you can see the board separated from the assembly.

main board Tivoo apart.

Next Here I add some high resolutions from the front and back of the circuit board.

Front side of board
back side of board.

That will be all in this entry, thanks

Note: to see high res in your phone you may need to switch to desktop site mode.

Divoom Tivoo Retro Non Destructive INSIGHTS

For Black Friday I ordered a Divoom Tivo, since I think the pixel art on it looks so cool. And also to learn about the workings of this little neat device.

Divoom Tivo Inside box

The device itself is very nice.

Tivo showing Mario Pixel Art

The grid of the screen is a 16×16 pixels, and the whole screen measures 579×509. you can see a mock of the configuration in the following sketch.

Mock of Tivo

The values in the above image are my measures with caliper and may be a bit of and the may not even add, but they give a nice approximation. (good enough for me at least).

GAL16V8 :3- 4 bit counter using macrocell

We will construct a 4 bit counter using the GAL16V8, but instead of constructing the latch like in the previous post, LATCHES, SR LATCH. we will use the builtin flip flop of the MACROCELL.

As a quick reference, a D type flip flop is a device that can store one value, like the SR LATCH, however the value will only be stored on the rising edge of the clock. In reality it does the change using both the rising and falling edge of the clock. You can have a deeper insight of its works with this video from Ben Eater.

When configuring a macrocell you need to describe the equations which based in the current state, will change the next state.

When configuring the D-Latch you need to think on how the next state will be reached based on the current state. We can think of it as current state, Q and next state QNext.

In a 1 bit binary counter, when Q is 0, the next state is 1. and when Q is 1, we overflow and the next state is 0. Making it effectively that QNext is !Q, such equation would look like this.

QNext = !Q;

Lets see what would be the truth table for this 1 bit counter.

QQ Next

Now, the second less significant bit, Q2, will have its next state based on two values, the value of Q and the value of Q2. For the rest of the examples we will call Q as Q1.

The first condition of Q2 is that it will change every time Q1 transitions from 1 to 0, or effectively, every time !Q1.

We can see that in the following truth table.


Q2Next = (Q2 and !Q1) or (!Q2 and Q1)

In colloquial language this could be expressed as the following quote.

If Q1 is true, Q2Next will be complement of Q2, if Q1 is false, Q2 don’t change.

Now, the way to write this code in CUPL, to be able to progaram with our gal will be very similar, however CUPL does not have a QNext, instead it have a Q.d where d stands for DLatch. So our equation in CUPL will be like this for Q1 and Q2.

Q2.d = (!Q1 & Q0) # (Q1 & !Q0);
Q1.d = !Q0; 

We can follow the same analysis for the other 2 bits. If we decide to do so we could get a CUPL code similar to this one. This code also includes a clr input, to reset the counter to 0 at any time.

 Partno XXXXXX;
 Date XX/XX/XX;
 Revision XX;
 Designer XXXXXX;
 Company XXXXXX;
 Assembly XXXXXX;
 Location XXXXXX;
 Device g16v8ms
 PIN 1 = clk;
 PIN 2 = clr;
 PIN 15 = Q0;
 PIN 14 = Q1;
 PIN 13 = Q2;
 PIN 12 = Q3;
 Q3.d = !clr & ((!Q3 & Q2 & Q1 & Q0) # (Q3 & !(Q2 & Q1 & Q0)));
 Q2.d = !clr & ((!Q2 & Q1 & Q0) # (Q2 & !(Q1 & Q0)));
 Q1.d = !clr & (!Q1 & Q0 # Q1 & !Q0);
 Q0.d = !clr & !Q0;

This would be the wiring diagram.

4 bit counter from aruduino

The clock is being generated by an arduino, but it could be generated by any means, including a 555 timer as long as is a square wave between 0 and 5 volts. (for this circuit that is being powered by 5V).

Also here we can see the the circuit assembled in action, I added a small yellow led to see the state of the clock.

GAL16V8 :2- Latches, SR Latch

Today we will explore the basic of SR Latch and how to build one with a GAL16V8.

The wikipedia definition of a latch is ” a circuit that has two stable states and can be used to store state information“.

The latch we want to implement today is the SR LATCH with two nor gates. the circuit is as follows.

Image result for sr latch

This latch have two inputs, Set (S) and Reset (r), and two exits, Q, which is the stored state, and !Q, which is the inverse of the stored state. Easy.

The truth table of this circuit is as follows.

Truth table of SD Latch

As we can see, as long as only one of either Set or Reset is on, the Q output will store the value of Set.

Note that when Set and Reset are off, Q and !Q keep their values, that is where the storage property comes in.
Also in this specific latch made out of NOR gates, when both Set and Reset are ON both Q and !Q are disabled, thus both are 0, this is an unstable state and the end value of Q and !Q when both Set and Reset are OFF again will be a race condition on which one is set last.

Now How will this looks in CUPL code? Well, we only need to define the following two equations.

Q = !(R # QN);
QN = !(S # Q);

As you can see, we are only describing the inputs of the NOR gates here, in the CUPL file it will look similar to this.

partNo 00;
Date 16/06/2016;
Revision 01;
Designer Esteban;
Company estebon;
Assembly None;
Location None;
Device g16v8ms;

PIN 2 = R;
PIN 3 = S;

PIN 12 = Q;
PIN 13 = QN;

Q = !(R # QN);
QN = !(S # Q);

Now we burn our gal and wire as follows. See the Make it blink tutorial on how to program our gal.

Wiring of SR Latch

And finally we can test it, Notice how the output led match the button pressed.

SR Latch with GAL16V8

That is all on this, hope you find it useful.

GAL16v8 :1- Jumpstarting, I want my gal to do something now!

Well, you can’t hahahah. just kidding.

This is a quick guide and I am assuming you can understand some basic boolean logic.

First you need 3 things:

1- A GAL16V8 of course. From EBAY? Sure.

2- A GAL programmer, from our chinese friends. This is the one I bought and seems to work fine so far and is kind of cheap.

3- winCUPL, you can get it from here

Fine, you installed all and got all the items. so what are we going to create? Well in this example we will create a pass thru and an inverter. basically a circuit like the following.

This is a circuit that have one Input, in leg 2 and two outputs, which is passthru in leg 12 and the negation operation in leg 13.

those follow the next boolean table


We are going to program this configuration with CUPL language using WinCUPL as follows.

Now Lets compile it using the Device Dependent Compile button, which will use the device specified in the Device tag, in this case g16v8 to create a jed file.

Success, now we will use the software that should have been included with your G540 Programmer as follows.

The next window will open and as long as it looks like this it should be good. just click ok.

Next step is to select your device, which if you bought the lattice gal GAL16V8D should be like this. First click the Select button and look for the device.

Finally, the programming window appear, place your gal in the programmer, (check that your programmer is conected to the pc) . Ensure the notch is in the correct direction and that the gal is placed at the base like this.

Then click Prog button and hopefully it will work.

Now Prepare a breadboard like this.

And test :3

the schematic is like this.


  • Resistor is 220 ohms
  • voltage is 5v, from an arduino :), bt any 5v source will do.
  • I didn’t put resistors in my LEDs but they may be needed ?

So this is the basic guide, I skip how to install the software, but hope it helps you to jumpstart into gals, I will explain more in detail in future post about this old and forgotten world of PLA (Programable logic Arrays) which GAL is part of :).

GAL 16V8 References :)

Have you decided you want to learn to code in gal but don’t know where to start. Well welcome, this page won’t explain a thing but here are nice links you can follow in your quest.

Good CUPL Language reference Guide

GAL16V8 DataSheet

The cheap gal programmer I bought, I have tried with GAL 16V8 from Lattice and works fine so far.

Where to download winCUPL “for windows” with bugs, but is free.

Software Developer and a bit more