
/* ////////////////////////////////////////////////////////////////////////////
				     mst.v
///////////////////////////////////////////////////////////////////////////////
DESCRIPTION:	This is the mark/space timer part of the Morse code decoder.
                It continuously monitors the input line, looking for
                transitions in either direction.  As each transition is
                detected, it records the the duration of the mark or space just
                ended (this is just the time since the previous transition),
                along with a single bit to indicate whether it was a mark or a
                space.  It then pulses a third output to inform the client
                modules that a new event has occurred.

REVISIONS:	28 Sep 07 - RAC - Adapted from an earlier attempt
//////////////////////////////////////////////////////////////////////////// */

module MarkSpaceTimer (clock, rawInput, eventType, eventDuration, newEvent);

    input  clock;				// 20 µs (50 KHz) clock
    input  rawInput;				// Raw Morse code signal
    output eventType;				// MARK (1) or SPACE (0)
    output eventDuration;			// Length of mark or space, in
						//  20 µs units
    output newEvent;				// Pulsed once for each event

    reg			    eventType;		// See above
    reg			    newEvent;		// See above
    reg [`TIME_WIDTH-1 : 0] eventDuration;	// See above
    reg [`TIME_WIDTH-1 : 0] timer;		// Accumulate event times here
    reg			    inputNow;		// Raw input, synced to clock
    reg			    inputBefore;	// Input delayed by one clock

    always @(posedge clock) begin		// Every 20 µs
	inputBefore <= inputNow;		// Note input at last clock
	inputNow <= rawInput;			// Note input right now
	if (inputNow == inputBefore) begin	// No transition
	    timer <= timer + 1;			// Just increment the timer
	    newEvent <= 1;			// Note that output is valid
	    end					// End 'no transition'
	else begin				// We have a transition
	    eventType <= inputBefore;		// Record event type
	    eventDuration <= timer;		// Record event duration
	    timer <= 1;				// Reset the timer
	    newEvent <= 0;			// Trigger output 20 µs hence
	    end					// End 'we have a transition'
	end					// End 'every 20 µs'

    endmodule					// End MarkSpaceTimer module

/* ////////////////////////////////////////////////////////////////////////////
				    mst_t.v
///////////////////////////////////////////////////////////////////////////////
DESCRIPTION:	This is a testbench for the MarkSpaceTimer module of the Morse
		decoder project.  It drives the module with a clock and some
		fake input.  You have to examine and interpret the output
		waveforms manually.

REVISIONS:	28 Sep 07 - RAC - Genesis
//////////////////////////////////////////////////////////////////////////// */

`timescale 1us / 1ns

`include "../modules/mcd.h"			// Grab global definitions
`include "../modules/mst.v"			// Grab module to test

module MstTester;

    reg			     clock;		// The 20 µs clock
    reg			     morseCode;		// The raw input signal
    wire		     eventType;		// Type of last event
    wire [`TIME_WIDTH-1 : 0] eventDuration;	// Duration of last event
    wire		     newEvent;		// New event clock

    MarkSpaceTimer DUT (clock, morseCode, eventType, eventDuration, newEvent);

    always begin				// Clock generator
	#10 clock <= 0;				// 10 µs down
	#10 clock <= 1;				// 10 µs up
	end					// End 'clock generator'

    initial begin				// Wiggle the input line up and
	#0   morseCode <= 0;			//  down a few times
	#101 morseCode <= 1;
	#300 morseCode <= 0;
	#100 morseCode <= 1;
	#100 morseCode <= 0;
	#100 morseCode <= 1;
	#300 morseCode <= 0;
	#100 morseCode <= 1;
	#100 morseCode <= 0;
	#100 morseCode <= 1;
	#300 morseCode <= 0;
	#100 morseCode <= 1;
	$finish;
	end

    endmodule
