COSMAC CDP1802 Processor


News:
8-14-2012
New version of C Compiler for 1802 to fix bug with signed compares and also to produce faster/smaller code for signed compares.
8-21-2011
New version of PIC code for 1802 emulator and 1861 emulator in the Elf Clone to fix bug with EF4 and EF2 in 1802 emulator and to clean up display on and off in 1861 emulator.
11-22-2010
New version 2.0 of Elf Clone which uses more common RAMs and discrete LEDs for port 4 to save money.  Also includes photo of soldered version.
4-16-2010
New version of Elf Clone which handles display on and display off correctly as well as fixed some timing issues in the 1861 emulator to deal with out of sync interrupt handlers.  Also, implemented fake 0x68 instructions to access PICs RS232 hardware so the 1802 now appears to have a 115K baud serial port.  This is quite nice to have.
3-11-2010
New version of Elf Clone which now includes 1861 emulator and new version of  C Compiler for 1802  which properly supports ROM code generation.
12-14-2009
Updated versions of C Compiler for 1802, and Elfos Terminal program examples.

Compiler:
  1. Recoded the variable variation code (address of, pre and post inc and dec, dereference) and am able to pass lists of 1000 random generated equations (compared to gcc).  Now pointeres and array indexing mixed with function calls works much better.  Ugly crap like "i = s[3+fart(s[3+9-fart(2,1)]+7,2)];"  compiles correctly now.
  2. Added some code optimizations for shifting, negative checks and function calls.
  3. General little bugs here and there have been fixed.  See the ToDo.txt file in the download for all the details.

C Code Examples:
  1. Most of the work has been in the ElfOs examples.  Added file system support, open, fclose, fgets, fputs and created some examples to use them.  elfos_more is like the unix more command and elfos_vi is a very primitive screen level editor.  Both of these are just starting points for real tools but the show that the ElfOs file system can be used from a C program.  elfos_filter is a primitive version of the unix tool grep.
  2. Added string.h file that has assembly language routines for strlen, strcpy, strstr and strcmp to deal with strings.
  3. Finished up the breakout game and it now is included in the examples



Links:

All Projects:
Hardware
Elf Clone
My First Computer
Logic Analyzer
Cassette Interface

Software
Emulator for My First Computer
C Compiler for 1802
Assembler/Disassembler
Rom Code
Tubes
Hinge
Clock


Hardware Projects:
Elf Clone:
Final Hardware
Running Graphics
PIC Development
Click Images for Larger Views
1802 emulator Code:  B1802_18F.asm (V0.09 Debug, V0.16 Emulator: 7-23-2011) Old Version 0.14
1802 emulator Intel Hex for programming PIC:  B1802_18F.hex (7-23-2011)  Old Version
1861 emulator Code:  B1861_18F.asm (V0.07 8-21-2011)
1861 emulator Intel Hex for programming PIC:  B1861_18F.hex (8-21-2011)
Schematic:  ElfClone.pdf (11-09-2010) Old Version
Breadboard Layout: Layout.pdf (Note that the two RAMs are stacked on top each other with one pin lifted.)
Devices: PIC18F4620 for 1802 and PIC18F2620 for 1861 (My Pic Page)
Simulator Results:  Simulator.zip
Hardware Results:  HW.zip

1802Control.zip (V0.03 3-09-2010)  PC side 1802 control program to download code for Elf Clone and also to access debug mode of the 1802 which allows new emulator code to be downloaded to flash memory and 1802 code to be written to the section of flash memory that is used to emulate a ROM.


My First Computer:


Click Image for Larger View
Code: In ROM, on cassete tape, and on paper.
Schematic: Not translated.  On paper
Device: CDP1802CD
This is my first computer.  Actually, this is a repackaging that I did in 1984 of my first computer that I made in 1977.  I have no pictures of the orignal which was constructed on perf board.  The hex keypad is an addition that I made in 1979.  Before that, I programmed in binary!
Logic Analyzer:

Click Image for Larger View
Code: In Rom and on paper
Schematic: Not translated on paper
Devices: CDP1802CD processor and CDP1861CD video generator
This project is an 16 channel 1K deep 5 megasample/sec logic analyzer that displays the resulting waves on a TV set or monitor.  There are two boards.  The one that is visible is the logic analyzer made from surplus TTL parts and some fast static RAMs (4 1K by 4-bit).  The lower board uses the 1802, 1861 and more TTL and CMOS parts to implement the user interface.  The 16 toggle switches on the front select the trigger event for each channel (1,0 or X center off).  The switch on the far left selects which edge of the event to trigger on.  The red pushbuttons control arming the trigger and displaying the results on the screen.

I won The Ohio State University design contest with this device during my last year of undergrad (1985).

Cassette Interface:

Follow this link to my PIC processor page for details: Cassette Interface


Software Projects:

Emulator:

Windows: TedsElf.zip Version 0.19 (9-8-2009)

This program emulates my first computer desccribed above.  Get ROM Code and Tubes for a cheap thrill.  The 8 LEds above the keypad are the PORT 4 display that I use.  The lit LED above that is the Q LED.  The [Y[[[KK display at the top left is my 7 digit ASCII display.  See Clock below for a program to make it wiggle.  The S and H keys on the keypad do nothing.  The L key is the Input key (Load button).  I assert EF3 on any hex key down and EF4 on the input key down.  The Auto Load switch will pulse the input button every other hex key press.

To load a simple program using the Hex key pad.  Click Reset, then Load, then key in 7 B 3 0 0 0.  Then click Reset then Run after making sure the run addresss is set to zero.  The Q LED should light.  For more fun, repeat the procedure but use:

E2 F8 01 B2 13 93 52 64 22 30 04
I use the windows timer function (interrupts) to update the gui at a rate of about 50 frames per second.  After each update I let the1802 run for about 2000 instructions.  I foget the exact numbers but I set it up to match the speed of an ELF running at 3.58MHz/2.  The video animation rates seem to match my real machine pretty well and run smoothly.  I simulate the 1861 interrupts and DMA access (and EF1 pulse) so that the frame buffer and resolution can be controlled under software control like a real machine.  I've only tested 64x64 (difficult) mode and 64x128.  I have not tested 64x32 mode but I expect it to work.  I used 64x128 mode for my Logic Analyzer.

The Debug Control window shown above can be opened up by selecting the View menu then selecting Debug Window.  A single break point can be set and then the processor can be single stepped.  The display shows the processor stopped in the video interrupt routine (P=1) with the 1861 Pixie chip currently set to DMA the line starting at address 0x01b0 (R0).  It is not real pretty, but if you have the assember listing up next to this debugger window it makes it pretty easy to debug programs.

The file load function accepts hex files produced with my Assembler which will include a run address and and optional description.  Intel hex file format is also supported but there is no way to embed the start address or description.

C Compiler:
Windows Command Line plus examples: cc1802.zip Version 1.03 (7-21-2012)
Linux Command Line plus examples:  cc1802.tar.gz Version 1.00 (3-09-2010)
The output is a single .s file that can be assembled with this assembler.

  Usage: cc1802 [options] filename
  -I include_dir       Add include_dir for directory search
  -elf_os              Produce code to run under Elf/OS (starting at 0x2000)
  -rom                 Generate code for a ROM
                       Requires use of -data_start and -code_start and
                       optionally -video_start and video_size for video
                       space and optionally -stack_size
  -nlb                 Use no long branches (required for 1861 video)
  -mem_config Config   Set memory configuration (Default is 0)
    0: {Stack, Program}       (Set stack size)
    1: {Program,Stack}        (Set stack size)
    2: {Video,Stack,Program}  (Set video size, and stack size)
    3: {Stack,Video,Program}  (Set video size and stack size)
    4: {Program,Stack,Video}  (Set stack size plus video size and optionally video start)
  -stack_size Size     Size in bytes (default is 256)
  -video_start Addr    Address to start video memory (should snap to page)
  -video_size Size     (default is 512 for single 64x64 display buffer)
  -mem_start Addr      First address to populate (default is 0)
  -code_start Addr     First address for instructions (rom mode only)
  -data_start Addr     First address for stack and data (rom mode only)

The -mem_config and -video_start exist to get the code to run on different systems where monitor programs trash memory while loading programs.  My monitor uses page zero for variables and page one and two for video memory, so I use -mem_config 3 and use the defaults.  This places the stack in page zero and 1861 video memory in pages one and two.  The program starts at location 0x0300.  For programs that do not use video memory,  use -mem_config 0 -stack_size 0x300 to give 768 bytes of stack and a program that starts at location 0x300.

Programs that use the 1861 must use the -nlb option so that no long branches are generated.  My work-around to this problem is a little ugly but does the job.

Check out the example flags.c to see how to deal with input and output ports as well as the external flags and Q.  Here is the source:

void main()
{
    unsigned char input,flag4;

    while(1){
        flag4 = EF4;
        if(EF3){
            input = INP4 & 7;
            if(input==1) Q = 1;
            if(input==0) Q = 0;
        }
        OUT4 = (Q<<7) | (input<<4) | (EF4<<3) | (EF3<<2) | (EF2<<1) | EF1;
    }
}

A word of caution about syntax errors.  Please fix the first error encountered as currently the compiler does not recover very well from a prior error so it may complain about a valid line because it is in a confused state.  The biggest problem that may show up is forgetting to declare a function as returning a type.  All functions must be declared to either return a defined type or void.

Features that still need to be implemented

  • Conditional operator a = (b>3) ? 25 : 17;
    • I'm not sure how to do this but it seems like the equation engine has to deal with this.
    • Similar to if(b>3){ r7=25 }else{ r7=17}
    • I hope this is very similar to implementing function calls and arrays in equations.
  • Implement a+=3, a-=3, a*=5 ...
  • Finish up remaining keyword implementations
    • goto plus lables
    • sizeof
Features that are not currently supported but may be one day
  • handle double triple .. arrays [][][]
  • handle structures and typedefs
  • Handle long
  • Handle float
Assembler/Disassembler:
Windows Command Line: AsmDsm1802.zip Version 1.00 (3-09-2010) / Version 0.22 (12-10-2010)
Source: AsmDsmSource.zip
Manual: asm1802.html
Three output formats are supported (-format_intel outputs intel hex format, -format_ted outputs hex format for TedsElf emulator, -format_elf_os outputs binary format for Elf/OS).

I grew up programming my Elf in Hex as it was my only computer.  Now that I have other machines I broke down and wrote a lame disassembler and assembler.  I do not like the mnemonics that RCA came up with so created my own which for me are easier to use.  The assembler supports RCAs mnemonics as well so you don't have to use the new names.  For example, I use ld instead of LDN, LDA, LDX, LDXA, LDI, GLO and GHI and figure out what op-code to generate based on the source format.  Here are some examples:

0032 0x001c 0x43                   ld      *r3++    ;LDA
0033 0x001d 0x05                   ld      *r5      ;LDN
0034 0x001e 0xf0                   ld      *rx      ;LDX
0035 0x001f 0x72                   ld      *rx++    ;LDXA
0036 0x0020 0xf8 0x09              ld      9        ;LDI
0037 0x0022 0xf8 0x0a              ld      Piddle.0 ;LDI (Low 8 bits)
0038 0x0024 0x8e                   ld      r14.0    ;GLO
0039 0x0025 0x97                   ld      r7.1     ;GHI
Around 1990 I wrote an assembler for a TI DSP that used a similar format (TI's invention not mine) but I think it is much easer to read and understand what the code is doing.  See my TI DSP projects here.

The disassembler can read Intel Hex format or my annotated Hex format that comes out of my Cassette Interface project which uses a PIC processor.  Every geek should learn about these parts as they are a way of life.

Simple little program to get the PORT 4 LEDs to count
 

                .title "Count"
        .desc "This program counts using the output"
        .desc "display."

        .start Main ; Start address

        .org 0x0000

Main:   sex 2
        ld 1
        st r2.1
Loop:   inc r3
        ld r3.1
        st *r2
        out 4
        dec r2
        br Loop


ROM Code:

Source: None yet.  I only have a file dump from my cassette interface as the source is pencil and paper
Hex File: Rom.zip
This project contains the monitor program shown in the picture above for my first computer.  It also contains functions to draw circles, lines, boxes, do a little math and drive the ASCII display.  This code is required to run most of the programs.

To run the monitor, start execution at address 0xf000.  Turn off the auto load function and use the 4 and 6 keys to select the desired function.  For the emulator, only Edit, Find, Move and Run make sense.  Once the desired selection is lit pulse the Input key (L on the emulator).  For Edit, enter a 4 digit address to edit.  The 2,4,6 and 8 keys control the cursor position.  Use E to exit.  Pulse the Input switch to enter edit mode.  When in Edit mode, enter hex digits to change memory.  When finished, pulse the Input switch to return to cursor move mode.  Click the E to exit then select RUN and enter a 4 digit run address followed by pulsing the input switch to confirm in order to run code at the selected location.

Tubes:
Source: Tubes.s (Translated from notes.  Originally written in 1985)
Hex File: Tubes.zip
This program draws circles on the screen with a trail that is erased as the code runs. This program requires the ROM Code to run.  Run the program from address 0x400.  For the emulator, first load the ROM code then Tubes.hex and click the Run button.
Hinge:
Source: None yet.  Currently just pencil on paper (I wrote this back in 1985)
Hex File: Hinge.zip
This program draws a big hinge on the screen with a trail that is erased as the code runs. This program requires the ROM Code to run.  Run the program from address 0x400.  For the emulator, first load the ROM code then Hinge.hex and click the Run button.
Clock:
Source: Clock.s
Hex File: Clock.zip
This program uses the real-time clock at location 0xd000 to 0xd004 to display the current time on the ASCII display.  This program requires the ROM Code to run.  Run the program from address 0x300.  For the emulator, first load the ROM code then Clock.hex and click the Run button.  It is not possible to set the time using the emulator as the emulator gets the time from the Windows OS.

Last Updated August 14th, 2012
Home
Send Comments