analyzer.c

00001 /* Simple Logical Analyzer
00002    Copyright (C) 2001, 2002 Free Software Foundation, Inc.
00003    Written by Stephane Carrez (stcarrez@nerim.fr)       
00004 
00005 This file is free software; you can redistribute it and/or modify it
00006 under the terms of the GNU General Public License as published by the
00007 Free Software Foundation; either version 2, or (at your option) any
00008 later version.
00009 
00010 In addition to the permissions in the GNU General Public License, the
00011 Free Software Foundation gives you unlimited permission to link the
00012 compiled version of this file with other programs, and to distribute
00013 those programs without any restriction coming from the use of this
00014 file.  (The General Public License restrictions do apply in other
00015 respects; for example, they cover modification of the file, and
00016 distribution when not linked into another program.)
00017 
00018 This file is distributed in the hope that it will be useful, but
00019 WITHOUT ANY WARRANTY; without even the implied warranty of
00020 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00021 General Public License for more details.
00022 
00023 You should have received a copy of the GNU General Public License
00024 along with this program; see the file COPYING.  If not, write to
00025 the Free Software Foundation, 59 Temple Place - Suite 330,
00026 Boston, MA 02111-1307, USA.  */
00027 
00050 #include "analyzer.h"
00051 
00052 #define TIMER_DIV  (8192L)
00053 #define TIMER_TICK (M6811_CPU_E_CLOCK / TIMER_DIV)
00054 
00055 /* Setup for a 8Mhz quartz, and prescaler set to 1
00056    (500ns period).  */
00057 #define USEC_PER_TICK (1)
00058 #define USEC_DIVISOR  (2)
00059 
00060 void
00061 collect_samples (sample_list_t *samples)
00062 {
00063   unsigned i;
00064   unsigned char current_port;
00065   sample_t *sample;
00066 
00067   sample = samples->first;
00068   current_port = _io_ports[M6811_PORTA];
00069   for (i = samples->length; i != 0; i--, sample++) {
00070     unsigned char c;
00071 
00072 #ifdef TEST
00073     unsigned k = 0;
00074 #endif
00075     do {
00076 #ifdef TEST
00077       if (k == 0) {
00078         _io_ports[M6811_PORTA] ^= 0x80;
00079       } else {
00080         k++;
00081       }
00082 #endif
00083       c = current_port ^ _io_ports[M6811_PORTA];
00084     } while (c == 0);
00085     sample->tick = get_timer_counter ();
00086     c = c ^ current_port;
00087     sample->port = c;
00088     current_port = c;
00089   }
00090 }
00091 
00092 void
00093 report_samples (sample_list_t *samples)
00094 {
00095   sample_t *sample;
00096   unsigned i;
00097   sample_t *prev;
00098   
00099   prev = samples->first;
00100   sample =  &prev[1];
00101   for (i = samples->length - 1; i != 0; i--, sample++) {
00102     unsigned dt;
00103     unsigned char c;
00104     unsigned long usec;
00105 
00106     c = sample->port;
00107     dt = sample->tick - prev->tick;
00108     usec = (((unsigned long) dt) * USEC_PER_TICK) / USEC_DIVISOR;
00109     printf ("%lu %lu %c %c %c\n",
00110             (long) dt, usec,
00111             (int) (c & 1 ? '1' : '0'),
00112             (int) (c & 2 ? '1' : '0'),
00113             (int) (c & 4 ? '1' : '0'));
00114     prev = sample;
00115   }
00116 }
00117 
00118 #define MAX_SAMPLES (256)
00119 
00120 sample_t sample_buffer[MAX_SAMPLES];
00121 
00122 int
00123 main ()
00124 {
00125   sample_list_t list;
00126 
00127   serial_init ();
00128 
00129 #ifdef TEST
00130   _io_ports[M6811_PACTL] |= M6811_DDRA7;
00131 #endif
00132   list.length = MAX_SAMPLES;
00133   list.first = sample_buffer;
00134   lock ();
00135   while (1)
00136     {
00137       collect_samples (&list);
00138       report_samples (&list);
00139     }
00140 }