Subversion Repositories svn.mios

Rev

Rev 1 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
44 tk 1
// $Id: eg.c 44 2008-01-30 21:39:30Z tk $
2
/*
3
 * EG program module
4
 *
5
 * ==========================================================================
6
 *
7
 * Copyright (C) 2005  Thorsten Klose (tk@midibox.org)
8
 *
9
 * ==========================================================================
10
 *
11
 * This file is part of a MIOS application
12
 *
13
 * This application is free software; you can redistribute it and/or modify
14
 * it under the terms of the GNU General Public License as published by
15
 * the Free Software Foundation; either version 2 of the License, or
16
 * (at your option) any later version.
17
 *
18
 * This application is distributed in the hope that it will be useful,
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
 * GNU General Public License for more details.
22
 *
23
 * You should have received a copy of the GNU General Public License
24
 * along with This application; if not, write to the Free Software
25
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26
 *
27
 * ==========================================================================
28
 */
29
 
30
/////////////////////////////////////////////////////////////////////////////
31
// Include files
32
/////////////////////////////////////////////////////////////////////////////
33
 
34
#include <cmios.h>
35
#include <pic18fregs.h>
36
 
37
#include "main.h"
38
#include "eg.h"
39
 
40
/////////////////////////////////////////////////////////////////////////////
41
// Global variables
42
/////////////////////////////////////////////////////////////////////////////
43
 
44
// in order to achive best performance, we are NOT using arrays here!
45
 
46
// external accessible variables
47
eg_state_t    eg0;
48
unsigned int  eg0_value;
49
unsigned char eg0_attack, eg0_decay, eg0_sustain, eg0_release;
50
 
51
// internal state (not externally available)
52
eg_int_state_t eg0_int;
53
 
54
// this includes the eg_add_table
55
// it was generated with: perl utils\eg_table.pl > eg_table.inc
56
#include "eg_table.inc"
57
 
58
 
59
/////////////////////////////////////////////////////////////////////////////
60
// This function initializes the EG module
61
// it should be called from USER_Init() in main.c
62
/////////////////////////////////////////////////////////////////////////////
63
void EG_Init(void)
64
{
65
  eg0.ALL     = 0x00;
66
  eg0_int.ALL = 0x00;
67
 
68
  eg0_value       = 0x00;
69
 
70
  eg0_attack  = 0x40;
71
  eg0_decay   = 0x40;
72
  eg0_sustain = 0x80;
73
  eg0_release = 0x40;
74
}
75
 
76
 
77
/////////////////////////////////////////////////////////////////////////////
78
// This function is periodically invoked from USER_Timer()
79
/////////////////////////////////////////////////////////////////////////////
80
void EG_Tick(void)
81
{
82
  // in order to simplify the handling, we are working with an unsigned value here
83
  // (C doesn't natively provide a carry flag :-/ )
84
  unsigned int eg_value;
85
  unsigned int lvl;
86
 
87
  if( eg0.GATE == 1 && eg0_int.LAST_GATE == 0 ) { // rising edge
88
    eg0_int.LAST_GATE = 1;
89
    eg0_int.STATE = ATTACK;
90
  } else if( eg0.GATE == 0 && eg0_int.LAST_GATE == 1 ) { // falling edge
91
    eg0_int.LAST_GATE = 0;
92
    eg0_int.STATE = RELEASE;
93
  }
94
  // eg0_int.LAST_GATE = eg0.GATE; // doesn't work with SDCC v2.5.0
95
 
96
  eg_value = eg0_value >> 1;
97
 
98
  switch( eg0_int.STATE ) {
99
    case IDLE:
100
      eg_value = 0x0000;
101
      break;
102
 
103
    case ATTACK:
104
      if( (eg_value += eg_add_table[eg0_attack]) & 0x8000 ) {
105
        eg_value = 0x7fff;
106
    eg0_int.STATE = DECAY;
107
      }
108
      break;
109
 
110
    case DECAY:
111
      lvl = (eg0_sustain << 7);
112
 
113
      if( ((eg_value -= eg_add_table[eg0_decay]) <= lvl) || (eg_value & 0x8000) ) {
114
        eg_value = lvl;
115
    //        eg0_int.STATE = SUSTAIN;
116
      }
117
      break;
118
 
119
    case SUSTAIN:
120
      eg_value = (eg0_sustain << 7);
121
      break;
122
 
123
    case RELEASE:
124
      if( (eg_value -= eg_add_table[eg0_release]) & 0x8000 ) {
125
        eg_value = 0;
126
    eg0_int.STATE = IDLE;
127
      }
128
      break;
129
  }
130
 
131
  eg0_value = eg_value << 1;
132
}
133