===============================================================================
              What you need to know about electronics telecards
===============================================================================

July 10 1993 / December 03 1993
Version 1.04
Stephane BAUSSON (sbausson@ensem.u-nancy.fr)
-------------------------------------------------------------------------------
   Any suggestions or comments about phonecards and smart-cards are welcome
-------------------------------------------------------------------------------

                              Content
                             ---------

I ) The cards from Gemplus, Solaic, Schlumberger, Oberthur:

    I-1) Introduction:
    I-2) SCHEMATICS of the chip:
    I-3) PINOUT of the connector:
    I-4) Main features:
    I-5) TIME DIAGRAMS:
    I-6) Memory MAP of the french phonecards:
    I-5) Memory MAP of the other cards:

II ) The cards from ODS: (German cards)

    II-1) Introduction:
    II-2) Pinout:
    II-3) Time Diagrams:

III) The Reader Schematic:

IV) The program:

-------------------------------------------------------------------------------

I ) The cards from Gemplus, Solaic, Schlumberger, Oberthur:
    =======================================================

    I-1) Introduction:
         ------------

  You must not think that the electronics phone-cards are completly secret
things, and that you can not read the informations that are inside. It is quite
false, since in fact an electronic phone-card does not contain any secret
information like credit cards and an electronic phonecard is nothing else that
an 256 bits EPROM, with serial output.

  Besides do not think that you are going to refilled them when you will have
understood how they work, since for that you should reset the 256 bits of the
cards by erasing the whole card. But the chip is coated in UV opaqued resin
even if sometime you can see it as tranparent! Even if you were smart enough to
erase the 256 bits of the card you should program the manufactuer area, but
this is quite imposible since these first 96 bits are writing protected by a
lock-out fuse that is fused after the card programing in factory.

  Neithertheless it can be very interesting to study how these cards work, to
see which kind of data are inside and how the data are maped inside or to see
how many units are left inside for exemple. Besides there are a great number of
applications of these cards when there are used (only for personal usage of
course) , since you can use them as key to open a door, or you can also use them as
key to secure a program, etc ....

  These Telecards have been created in 1984 and at this time constructors
decided to build these cards in NMOS technology but now, they plan to change by
1994 all readers in the public to booths and use CMOS technology. Also they
plan to use EEPROM to secure the cards and to add many usefull infornations in,
and you will perhaps use phone cards to buy you bread or any thing else.

These cards are called Second Generation Telecards.


    I-2) SCHEMATICS of the chip:
         ----------------------

                    .-------------------.
                    |                   |
                  --|> Clk              |
                    | _                 |
                  --| R/W               |
                    |                   |
                  --| Reset             |
                    |                   |
                  --| Fuse              |
                    |                   |
                  --| Vpp               |
                    |                   |
                    |                   |
                    '-.               .-'
                      |               |
                    .-------------------.
                    |               Out |-- serial output
                    '-------------------'


    I-3) PINOUT of the connector:
        -------------------------


          AFNOR CHIP                                   ISO CHIP
          ----------                                   --------

 -------------+-------------                 -------------+-------------
|   8         |         4   |               |   1         |         5   |
|             |             |               |             |             |
+-------\     |     /-------+               +-------\     |     /-------+
|   7    +----+----+    3   |               |   2    +----+    +    6   |
|        |         |        |               |        |         |        |
+--------|         |--------+               +--------|         |--------+
|   6    |         |    2   |               |   3    |         |    7   |
|        +    +----+        |               |        +----+----+        |
+-------/     |     \-------+               +-------/     |     \-------+
|   5         |         1   |               |   4         |         8   |
|             |             |               |             |             |
 -------------+-------------                 -------------+-------------


PINOUT:    1 : Vcc = 5V        5 : Gnd
------     2 : R/W             6 : Vpp = 21V
           3 : Clock           7 : I/O
           4 : Reset           8 : Fuse

    I-4) Main features:
        ---------------

        - Synchronous protocol.
        - N-MOS technology.
        - 256x1 bit organisation.
        - 96 written protected by a lock-out fuse.
        - Low power 85mW in read mode.
        - 21 V programming voltage.
        - Access time: 500ns
        - Operating range: -100C +700C
        - Ten year data rentention.


    I-5) TIME DIAGRAMS:
        ---------------

+21V                                     _____________
+5V ____________________________________|             |_________________ Vpp
                                        :             :
+5V                  ___________________:_____________:_________________ Reset
0V  ________________|                   :             :
                    :                   :             :
+5V     ____        :      ____         :       ______:______
0V  ___|    |_______:_____|    |________:______|      :      |__________ Clock
       :    :       :     :    :        :      :      :      :
+5V    :    :       :     :    :        :______:______:      :           _
0V  ___:____:_______:_____:____:________|      :      |______:__________ R/W
       :    :       :     :    :        :      :      :      :
+5V    :    :       :_____:    :________:      :      :      :__________
0V  XXXXXXXXXXXXXXXXX_____XXXXXX________XXXXXXXXXXXXXXXXXXXXXX__________ Out
       :    :       :     :    :        :<-----><---->:      :
       :    :       :     :    :        :10 to   10 to       :
       :    :       :     :    :        :50 ms   50ms        :
        Reset        Bit 1        Bit2                           Bit 3
        card        reading      reading  Bit2 writing to 1     reading



    I-6) MEMORY MAP of the french CARDS:
        --------------------------------

Bytes       Bits      Binary     Hexa

                    +-----------+-----+
  1        1 --> 8  |           |     |
                    +-----------+-----+
  2       9 --> 16  | 0000 0011 | $03 | ---> a french telecard
                    +-----------+-----+
  3      17 --> 24  |           |     |
                    +-----------+-----+
  4      25 --> 32  |           |     |
                    +-----------+-----+
  5      33 --> 40  |           |     |
                    +-----------+-----+
  6      41 --> 48  |           |     |
                    +-----------+-----+
  7      49 --> 56  |           |     |
                    +-----------+-----+
  8      57 --> 64  |           |     |
                    +-----------+-----+
  9      65 --> 72  |           |     |
                    +-----------+-----+
 10      73 --> 80  |           |     |
                    +-----------+-----+
 11      81 --> 88  |           |     |
                    +-----------+-----+
 12      33 --> 40  | 0001 0011 | $13 | ---> 120 units card
                    | 0000 0110 | $06 | --->  50 units card
                    | 0000 0101 | $05 | --->  40 units card
                    +-----------+-----+
 13-31  97 --> 248  |           |     | ---> The units area: each time a unit
                    |           |     |      is used, then a bit is set to "1";
                    |           |     |        Generaly the first ten units are
                    |           |     |      fused in factory as test.
                    |           |     |
                    |           |     |
                    |           |     |
                    +-----------+-----+
 32    249 --> 256  | 1111 1111 | $FF | ---> the card is empty
                    +-----------+-----+



    I-7) MEMORY MAP of the other cards:
        -------------------------------

Bytes       Bits      Binary     Hexa

                    +-----------+-----+
  1        1 --> 8  |           |     |
                    +-----------+-----+
  2       9 --> 16  | 1000 0011 | $83 | ---> a telecard
                    +-----------+-----+-----------+-----+
3-4      17 --> 32  | 1000 0000 | $80 | 0001 0010 | $12 | ---> 10 units card
                    |           |     | 0010 0100 | $24 | ---> 22 units card
                    |           |     | 0010 0111 | $27 | ---> 25 units card
                    |           |     | 0011 0010 | $32 | ---> 30 units card
                    |           |     | 0101 0010 | $52 | ---> 50 units card
                    |           |     | 1000 0010 | $82 | ---> 80 units card
                    | 1000 0001 | $81 | 0000 0010 | $02 | ---> 100 untis card
                    |           |     | 0101 0010 | $52 | ---> 150 units card
                    +-----------+-----+-----------+-----+
  5      33 --> 40  |           |     |
                    +-----------+-----+
  6      41 --> 48  |           |     |
                    +-----------+-----+
  7      49 --> 56  |           |     |
                    +-----------+-----+
  8      57 --> 64  |           |     |
                    +-----------+-----+
  9      65 --> 72  |           |     |
                    +-----------+-----+
 10      73 --> 80  |           |     |
                    +-----------+-----+
 11      81 --> 88  |           |     |
                    +-----------+-----+
 12      89 --> 96  | 0011 0000 | $30 | ---> Norway
                    | 0011 0011 | $33 | ---> Andorra
                    | 0011 1100 | $3C | ---> Ireland
                    | 0100 0111 | $47 | ---> Portugal
                    | 0101 0101 | $55 | ---> Czech Republic
                    | 0101 1111 | $5F | ---> Gabon
                    | 0110 0101 | $65 | ---> Finland
                    +-----------+-----+
 13-31  97 --> 248  |           |     | ---> The units area: each time a unit
                    |           |     |      is used, then a bit is set to "1";
                    |           |     |        Generaly the first two units are
                    |           |     |      fused in factory as test.
                    |           |     |
                    |           |     |
                    +-----------+-----+
 32    249 --> 256  | 1111 1111 | $FF | ---> the card is empty
                    +-----------+-----+



II ) The cards from ODS, Giesecke & Devrient, ORGA Karten systeme,
     =============================================================
     Uniqua, Gemplus, Schlumberger and Oldenbourg Kartensysteme:
     ===========================================================

    II-1) Introduction:
          ------------

        These cards are in fact 128 bit memory in NMOS technology, and the
map of these cards are the following:

               64 bit EPROM written protected (manufaturer area)
               40 bit EEPROM (5x8 bits)
               24 bits set to "1"


    II-2) Pinout:
         --------

  -------------+-------------
 |   1         |         5   |        Pinout:
 |             |             |        -------
 +-------\     |     /-------+
 |   2    +----+    +    6   |          1 : Vcc = 5V    5 : Gnd
 |        |         |        |          2 : Reset       6 : n.c.
 +--------|         |--------+          3 : Clock       7 : I/O
 |   3    |         |    7   |          4 : n.c.        8 : n.c.
 |        +----+----+        |
 +-------/     |     \-------+      n.c. : not connected
 |   4         |         8   |
 |             |             |
  -------------+-------------


    II-3) Main features:
         ---------------

        - ISO 7816- 1/2 compatible.
        - use a single 5V power supply.
        - low power consuption.
        - NMOS technology.

    II-4) Time Diagrams:
        ----------------

Reset:
------
  The address counter is reset to 0 when the clock line CLK is raised while
the control line R is high. Note that the address counter can not be reset
when it is in the range 0 to 7.

      __________________
_____|                  |_____________________________________________ Reset
     :                  :
     :        _____     :  _____       _____       _____       _____
_____:_______|     |____:_|     |_____|     |_____|     |_____|     |_ Clk
     :       :          : :     :     :     :     :     :     :     :
_____:_______:__________:_:_____:_____:_____:_____:_____:_____:_____:_
_____:___n___|_____0____:_|_____1_____|_____2_____|_____3_____|___4_:_ (Address)
     :                  :       :           :           :           :
_____:                  :_______:___________:___________:___________:_
_____XXXXXXXXXXXXXXXXXXXX_______|___________|___________|___________|_ Data
Bit n                      Bit 0    Bit 1        Bit2       Bit3

   The address counter is incremented by 1 with each rising edge of the clock
signal Clk, for as long as the control line R remains low. The data held in
each addressed bit is output to I/O contact each time Clk falls. It is not
impossible to decrement the address counter, therefore to address an earlier
bit, the address counter must be reset then incremented to require value.


Write:
------
   All unwritten or erased bits in the address 64-104 may be unwritten to.
When a memory cell is unwritten to, it is set to 0. The addressed cell is
unwritten to by the following sequence.

1- R is raised while Clk is low, to disable address counter increment for one
clock pulse.

2- Clk is then raised for a minimum of 10ms to write to the address bit.

When to write operation ends, and Clk falls, the address counter is unlocked,
and the content of the written cell, which is now 0, is output to I/O contact
if the operation is correct.
The next Clk pulse will increment the address by one, then the write sequence
can be repeated to write the next bit.

              _____                                _____
_____________|     |______________________________|     |_______________  Reset
             :                                    :
     ___     :           _____           ___      :           _____
____|   |____:__________|     |_________|   |_____:__________|     |____  Clk
    :        :          :     :         :   :     :          :     :
____:________:__________:_____:_________:___:_____:__________:_____:_____
 n  |      n+1          |     n+2       |   :    n+3         |     :       (Address)
----'--------:----------'-----:---------'---:-----:----------'-----:-----
             :          :     :             :     :          :     :
_________   _:          :     : ____________:  ___:          :     :
_________XXX_XXXXXXXXXXXXXXXXXXX____________ XX___XXXXXXXXXXXXXXXXXXXXXXX  I/O
   n      n+1           :     :     n+1        n+2           :     :
                        :     :                              :     :
                         write                                write


WriteCarry:
-----------

   A counter is erased by performing the WRITECARRY sequence on the stage of
the next highest weighing to that to be erased.

The writecarry sequence is as follows:

1 - Set the address counter to an unwritten bit in the next highest counter
stage to that to be erased.

2 - Increment is disabled on the following rising edge of R where Clk remains
low.

3 - Clk is then raised for a minimum of 10ms, while R is low, to write to the
next address bit.

4 - R is the raised again while Clk remains low to disable increment a second
time.

5 - Clk is the raised for a minimum of 1ms, while R is low, to write to the
addressed bit a second time, erasing the counter level immediately below that
the addressed bit.

       _____                      _____
______|     |____________________|     |_________________________________  Rst
      :                          :
      :            _______       :              _______        ___
______:___________|       |______:_____________|       |______|   |______  Clk
      :           :       :      :             :       :      :   :
      :           :       :      :             :       :      :   :
<------------------------- address n ------------------------>:<--- n+1 ------
      :           :       :      :             :       :          :
      :           :       :      :             :       :          :
______:           :       :______:             :       :__________: _____
______XXXXXXXXXXXXXXXXXXXXX______XXXXXXXXXXXXXXXXXXXXXXX__________XX_____  I/O
                  :       :  n                 :       :     n        n+1
                  :       :                    :       :
                    Write                        Erase

     II-5) Memory Map:
         -------------

Bytes       Bits      Binary     Hexa

                    +-----------+-----+
  1       1 -->   8 |           |     |
  2       9 -->  16 |           |     |
  3      17 -->  24 |           |     |
  4      25 -->  32 |           |     | ---> Issuer area (written protected)
  5      33 -->  40 |           |     |
  6      41 -->  48 |           |     |
  7      49 -->  56 |           |     |
  8      57 -->  64 |           |     |
                    +-----------+-----+
  9      65 -->  72 |           |     | ---> c4096  )
 10      73 -->  80 |           |     | --->  c512  )
 11      81 -->  88 |           |     | --->   c64  ) 5 stage abacus counter
 12      89 -->  96 |           |     | --->    c8  )
 13      97 --> 104 |           |     | --->    c0  )
                    +-----------+-----+
 14     105 --> 112 |           |     |
 15     113 --> 120 |           |     | ---> area of bits set to "1"
 16     120 --> 128 |           |     |
                    +-----------+-----+

The Issuer area:
----------------

    This issuer consists of 40 bits. The contents of the issuer area are
specified by the card issuer, and are fixed during the manufacturing process.
The contents of the issuer area will include data such as serial numbers,
dates, and distribution centers.

This area may only be read.

The Counter area:
-----------------

   The counter area stores the card's units. Its initial value is specified
by the card issuer and set during manufacturing.

The counter area is divided into a 5 stage abacus.

Note that you can only decrease the counter and it is not authorised to write
in the counter a value greater than the old value.


    I-6) Electrical features:
        --------------------

Maximum ratings:
----------------

                       +--------+------+------+------+
                       | Symbol | Min  |  Max | Unit |
+----------------------+--------+------+------+------+
| Supply voltage       |   Vcc  | -0.3 |    6 |   V  |
+----------------------+--------+------+------+------+
| Input voltage        |   Vss  | -0.3 |    6 |   V  |
+----------------------+--------+------+------+------+
| Storage temperature  |  Tstg  |  -20 |  +55 |  0C  |
+----------------------+--------+------+------+------+
| Power dissipassion   |    Pd  |   -  |   50 |  mW  |
+----------------------+--------+------+------+------+


DC caracteristics:
------------------
                            +--------+-----+-----+-----+------+
                            | Symbol | Min.| Typ.| Max.| Unit |
+---------------------------+--------+-----+-----+-----+------+
| Suplly current            |   Icc  |  -  |  -  |   5 |   mA |
+---------------------------+--------+-----+-----+-----+------+
| Input Voltage (low)       |    Vl  |   0 |  -  | 0.8 |    V |
+---------------------------+--------+-----+-----+-----+------+
| Input voltage (high)      |    Vh  | 3.5 |  -  | Vcc |    V |
+---------------------------+--------+-----+-----+-----+------+
| Input current R           |    Ih  |  -  |  -  | 100 |   uA |
+---------------------------+--------+-----+-----+-----+------+
| Input current Clk         |    Il  |  -  |  -  | 100 |   uA |
+---------------------------+--------+-----+-----+-----+------+
| Output current (Vol=0.5V) |   Iol  |  -  |  -  |  10 |   uA |
+---------------------------+--------+-----+-----+-----+------+
| Output current (Voh=5V)   |   Ioh  |  -  |  -  | 0.5 |   mA |
+---------------------------+--------+-----+-----+-----+------+

AC caracteristics:
------------------     +--------+------+------+------+
                       | Symbol | Min. | Max. | Unit |
+----------------------+--------+------+------+------+
| Pulse duration       |    tr  |   50 |   -  |  us  |
| R address reset      |        |      |      |      |
+----------------------+--------+------+------+------+
| Pulse duration       |    ts  |   10 |   -  |  us  |
| R write              |        |      |      |      |
+----------------------+--------+------+------+------+
| High level Clk       |    th  |    8 |   -  |  us  |
+----------------------+--------+------+------+------+
| Low level Clk        |    tl  |   12 |   -  |  us  |
+----------------------+--------+------+------+------+
| Write window         | Twrite |   10 |   -  |  ms  |
+----------------------+--------+------+------+------+
| Erase window         | Terase |   10 |   -  |  ms  |
+----------------------+--------+------+------+------+
|                      |   tv1  |    5 |   -  |  us  |
+----------------------+--------+------+------+------+
|                      |   tv2  |  3.5 |   -  |  us  |
+----------------------+--------+------+------+------+
|                      |   tv3  |  3.5 |   -  |  us  |
+----------------------+--------+------+------+------+
|                      |   tv4  |  3.5 |   -  |  us  |
+----------------------+--------+------+------+------+
|                      |   tv5  |  3.5 |   -  |  us  |
+----------------------+--------+------+------+------+
|                      |   tv6  |    5 |   -  |  us  |
+----------------------+--------+------+------+------+
|                      |   tv7  |    5 |   -  |  us  |
+----------------------+--------+------+------+------+
|                      |   tv8  |   10 |   -  |  us  |
+----------------------+--------+------+------+------+



III) The Reader Schematic:
    ======================

   External 5V (Optional)

5V o------,
          |                 /             T2  PNP      d13  r7 10
0V o--,   |                /               BC 177     |\ |  _____
      |   |      ,-------o/   o--*------. E      C .--| >+-[_____]--------,
    __+__ |      |               |       \        /   |/ |                |
    \\\\\ |    __|__ Batery      |         \    /                         |
          |      -   22.5V       |       ---------                        |
.......   |      |               |   _____   |   _____                    |
       :  |    __+__             +--[_____]--*--[_____]--,                |
   D2  :  |    \\\\\                r6 150k     r5 15k   |                |
4 o-------|---------------------------*------------------|-------------,  |
       :  |                           |   r3 220k       / C            |  |
   Ack :  |                           |   _____      |/    T1 - NPN    |  |
10 o------|--------.                  '--[_____]-*---|      BC107      |  |
       :  |        |                      _____  |   |\                |  |
       : ,-,      ,-,                 +--[_____]-'      \ E            |  |
       : | |r2    | |r1               |  r4 390k         |             |  |
       : | |220   | |22k            __+__              __+__           |  |
       : |_|      |_|               \\\\\              \\\\\           |  |
       :  |  |\ |  |                                                   |  |
       :  *--| >+--|----------------*----------------------------------|--*
       :  |  |/ |  |          ,-----|-----------------------------,    |  |
       :  |  d1    |          |     |   ,----------,----------,   |    |  |
       :  |        |          |     *---|--*  Fuse | Reset *--|---'    |  |
       :  |        |          |     |   |----------|----------|        |  |
   D0  :  |        |          |   ,-|---|--*   I/O | Clk   *--|---,    |  |
2 o-------|--------|----------'   | |   |----------|----------|   |    |  |
       :  |        |              | '---|--*   Vpp | R/W   *--|---|----'  |
  Busy :  |        |              |     |----------|----------|   |       |
11 o------|--------|--------------' ,---|--*   Gnd | 5V    *  |   |       |
       :  |        |                |   '----------'-------|--'   |       |
   D1  :  |        |              __+__    Chip connector  |      |       |
3 o-------|--------|--------,     \\\\\                    |      |       |
       :  |        |        '------------------------------|------'       |
  Str  :  |  |\ |  |                                       |              |
1 o-------*--| >+--*----*----*----*----*-------------------'              |
       :   d2|/ |  |d3  |d4  |d5  |d6  |d7                                |
       :          -+-  -+-  -+-  -+-  -+-                                 |
       :          /_\  /_\  /_\  /_\  /_\                                 |
   D3  :           |    |    |    |    |   |\ | d8                        |
5 o----------------*----|----|----|----|---| >+-------*-------------------'
       :                |    |    |    |   |/ |       |
       :                |    |    |    |              |
   D4  :                |    |    |    |   |\ | d9    |
6 o---------------------*----|----|----|---| >+-------*
       :                     |    |    |   |/ |       |
       :                     |    |    |              |
   D5  :                     |    |    |   |\ | d10   |
7 o--------------------------*----|----|---| >+-------*
       :                          |    |   |/ |       |
       :                          |    |              |
   D6  :                          |    |   |\ | d11   |
8 o-------------------------------*----|---| >+-------*
       :                               |   |/ |       |
       :                               |              |
   D7  :                               |   |\ | d12   |
9 o------------------------------------*---| >+-------'
       :                                   |/ |
       :
       :
25 o------.
       :  |
.......:  |                                 d1 to d13: 1N4148
        __+__
        \\\\\

Centronic port




IV) The program:
    ===========

    The following program will enable you to read telecards on you PC if you
build the precedent reader.

--------------- cut here (begin)
{*****************************************************************************}
{                             T E L E C A R D . PAS                           }
{*****************************************************************************}
{   This program enable you to dump the memory of electronics phonecards      }
{   from all over the world, so that you will be able to see which country    }
{   the card is from how many units are left and so on ....                   }
{*****************************************************************************}
{                                                                             }
{                        Written by Stephane BAUSSON (1993)                   }
{                                                                             }
{                         Email: sbausson@ensem.u-nancy.fr                    }
{                                                                             }
{                       Snail Mail Address: 4, Rue de Grand                   }
{                                           F-88630 CHERMISEY                 }
{                                           France                            }
{                                                                             }
{*****************************************************************************}

USES crt,dos;

TYPE string8=string[8];

VAR reg         : registers;
    i,j         : integer;
    bb          : array[1..32] of string8;
    bh          : array[1..32] of byte;
    l           : array[1..256] of boolean;
    car         : char;
    byte_number : integer;
    displaying  : char;

{-----------------------------------------------------------------------------}

PROCEDURE writeln_binaire(w:byte);

  BEGIN if (w and $80)=$80 then write('1') else write('0');
        if (w and $40)=$40 then write('1') else write('0');
        if (w and $20)=$20 then write('1') else write('0');
        if (w and $10)=$10 then write('1') else write('0');
        if (w and $08)=$08 then write('1') else write('0');
        if (w and $04)=$04 then write('1') else write('0');
        if (w and $02)=$02 then write('1') else write('0');
        if (w and $01)=$01 then write('1') else write('0');
        writeln;
  END;

{-----------------------------------------------------------------------------}

PROCEDURE send(b:byte);

  BEGIN reg.AH:=$00;
        reg.AL:=b;
        reg.DX:=0;
        intr($17,reg);
  END;

{-----------------------------------------------------------------------------}

FUNCTION get:byte;

  BEGIN reg.AH:=$02;
        reg.DX:=0;
        intr($17,reg);
        get:=reg.AH;
  END;

{-----------------------------------------------------------------------------}

FUNCTION units:byte;

  VAR  u,idx:integer;

  BEGIN u:=0;
        idx:=97;
        while (l[idx] and (idx<257)) do
        BEGIN inc(u);
              inc(idx);
        END;
        units:=u;
  END;

{-----------------------------------------------------------------------------}

PROCEDURE Card_Type;

  BEGIN case bh[2] of
         $03: BEGIN write('Telecard - France - ');
                    case bh[12] of
                     $13: write('120 Units - ',units-130,' Units left');
                     $06: write('50 Units - ',units-60,' Units left');
                     $15: write('40 Units - ',units-40,' Units left');
                    END;
              END;
         $83:BEGIN write('Telecard');
                   case bh[12] of
                     $30: write(' - Norway');
                     $33: write(' - Andorra');
                     $3C: write(' - Ireland');
                     $47: write(' - Portugal');
                     $55: write(' - Czech Republic');
                     $5F: write(' - Gabon');
                     $65: write(' - Finland');
                   END;
                   if (bh[12] in [$30,$33,$3C,$47,$55,$65]) then
                   BEGIN case ((bh[3] and $0F)*$100+bh[4]) of
                          $012: write (' - 10 Units - ',units-12,' Units left');
                          $024: write (' - 22 Units - ',units-24,' Units left');
                          $027: write (' - 25 Units - ',units-27,' Units left');
                          $032: write (' - 30 Units - ',units-32,' Units left');
                          $052: write (' - 50 Units - ',units-52,' Units left');
                          $067: write (' - 65 Units - ',units-62,' Units left');
                          $070: write (' - 70 Units - ',units-70,' Units left');
                          $102: write (' - 100 Units - ',units-102,' Units left');
                          $152: write (' - 150 Units -  ',units-152,' Units left');
                         END;
                    END;
{                    write(' - N0 ',bh[5]*$100+bh[6]);}
              END;
        END;
  END;

{-----------------------------------------------------------------------------}

PROCEDURE waiting;

  BEGIN send($00);
        write('Enter a card in the reader and press a key ...');
        repeat until keypressed;
        gotoxy(1, wherey);
        clreol;
  END;

{-----------------------------------------------------------------------------}

FUNCTION value(s:string8):byte;

  VAR  b:byte;

  BEGIN b:=0;
        if s[8]='1' then b:=b+$01;
        if s[7]='1' then b:=b+$02;
        if s[6]='1' then b:=b+$04;
        if s[5]='1' then b:=b+$08;
        if s[4]='1' then b:=b+$10;
        if s[3]='1' then b:=b+$20;
        if s[2]='1' then b:=b+$40;
        if s[1]='1' then b:=b+$80;
        value:=b;
  END;

{-----------------------------------------------------------------------------}

PROCEDURE write_hexa(s:string);

  VAR  i:integer;

  BEGIN if s='0000' then write('0') else
        if s='0001' then write('1') else
        if s='0010' then write('2') else
        if s='0011' then write('3') else
        if s='0100' then write('4') else
        if s='0101' then write('5') else
        if s='0110' then write('6') else
        if s='0111' then write('7') else
        if s='1000' then write('8') else
        if s='1001' then write('9') else
        if s='1010' then write('A') else
        if s='1011' then write('B') else
        if s='1100' then write('C') else
        if s='1101' then write('D') else
        if s='1110' then write('E') else
        if s='1111' then write('F');
  END;

{-----------------------------------------------------------------------------}

PROCEDURE full_displaying;

  BEGIN writeln('Memory dump:');
        for i:=1 to 80 do write('-');
        for i:=1 to (byte_number div 6 + 1) do
        BEGIN for j:=1 to 6 do
              BEGIN if j+6*(i-1) <= byte_number then write(bb[j+6*(i-1)]:9);
              END;
              gotoxy(60,wherey);
              for j:=1 to 6 do
              if j+6*(i-1) <= byte_number then
              BEGIN write_hexa(copy(bb[j+6*(i-1)],1,4));
                    write_hexa(copy(bb[j+6*(i-1)],5,4));
                    write(' ');
              END;
              writeln;
        END;
        for i:=1 to 80 do write('-');
        Card_Type;
        writeln;
  END;

{-----------------------------------------------------------------------------}

PROCEDURE short_displaying;

  VAR j : integer;

  BEGIN for j:=1 to byte_number do
        BEGIN write_hexa(copy(bb[j],1,4));
              write_hexa(copy(bb[j],5,4));
              write(' ');
        END;
        writeln;
  END;

{-----------------------------------------------------------------------------}

PROCEDURE reading;

  VAR i, j, k : integer;

  BEGIN send($FE);
        send($F8);
        k:=1;
        for i:=1 to 32 do
        BEGIN bb[i]:='';
              for j:=1 to 8 do
              BEGIN l[k]:=not((get and $08)=$08);
                    if l[k] then insert('1',bb[i],j) else insert('0',bb[i],j);
                    send($FB);
                    delay(1);
                    send($F8);
                    inc(k);
              END;
        END;
        for i:=1 to 32 do bh[i]:=value(bb[i]);
        case displaying of
          'F':full_displaying;
          'S':short_displaying;
        END;
  END;

{-----------------------------------------------------------------------------}

PROCEDURE writting;

  VAR i,n:integer;
      car:char;

  BEGIN write('Which bit do you want to set to "1" : ');
        readln(n);

        waiting;
        car:=readkey;

        send($FA);
        send($F8);
        for i:=1 to n do
        BEGIN send($F9);
              if i=n then
              BEGIN send($FD);
                    delay(20);
                    send($FF);
                    delay(20);
              END;
              send($FB);
        END;
        reading;
  END;

{-----------------------------------------------------------------------------}

PROCEDURE initialize;

  VAR i : integer;

  BEGIN byte_number:=32;
        displaying:='F';
        clrscr;
        writeln(' 1 - to dump a 256 bits card');
        writeln(' 2 - to dump a 128 bits card');
        writeln(' F - to display in full format');
        window(41,1,80,25);
        writeln(' S - to display in short format');
        writeln(' Q - to exit the program');
        window(1,4,80,25);
        for i:=1 to 80 do write('=');
        window(1,5,80,25);
  END;

{=============================================================================}

BEGIN initialize;
      repeat waiting;
             car:=upcase(readkey);
             case car of
              'W':writting;
              'Q':;
              '1':byte_number:=32;
              '2':byte_number:=16;
              'F','S':displaying:=car;
              else reading;
             END;
      until car='Q';
END.
--------------- cut here (end)
e reading;
             END;
      until car='Q';
END.
--------------- cut here (end)
