OSDN Git Service

* public snapshot of sid simulator
[pf3gnuchains/pf3gnuchains3x.git] / sid / demos / voice-pager / adac-driver.cxx
1 // adac-driver.cxx - description.  -*- C++ -*-
2
3 // Copyright (C) 1999, 2000 Red Hat.
4 // This file is part of SID and is licensed under the GPL.
5 // See the file COPYING.SID for conditions for redistribution.
6
7 #include "adac-driver.h"
8 #include "mem-map.h"
9 #include <iostream>
10
11 inline void
12 adac_driver::write_codec (enum codec_regs regno, unsigned value)
13 {
14   unsigned be_value;
15   unsigned char* be_value_bytes = (unsigned char *) & be_value;
16
17   be_value_bytes[0] = (value >> 24) & 0xFF;
18   be_value_bytes[1] = (value >> 16) & 0xFF;
19   be_value_bytes[2] = (value >> 8)  & 0xFF;
20   be_value_bytes[3] = (value >> 0)  & 0xFF;
21
22   codec_base [regno] = be_value;
23 }
24
25
26 inline unsigned
27 adac_driver::read_codec (enum codec_regs regno)
28 {
29   unsigned be_value;
30   unsigned char* be_value_bytes = (unsigned char *) & be_value;
31   unsigned value;
32
33   be_value = codec_base [regno];
34
35   value = ((be_value_bytes[0] << 24) | (be_value_bytes[1] << 16) 
36            | (be_value_bytes[2] << 8) | (be_value_bytes[3]));
37   return value;
38 }
39
40
41 adac_driver::adac_driver() 
42 {
43   tx_buf_ptr = 0;
44   tx_buf = "";
45   rx_buf = "";
46
47   codec_base = (unsigned*) CODEC_BASE;
48
49   write_codec (config, 134291264); // 8-bit uLaw mono 8000Hz
50 }
51
52
53 adac_driver::~adac_driver()
54 {
55   write_codec (tx_count, 0);
56   write_codec (rx_count, 0);
57 }
58
59
60 void
61 adac_driver::begin_read ()
62 {
63   write_codec (rx_count, -1);
64 }
65
66
67 void
68 adac_driver::poll_read ()
69 {
70   unsigned iterations = 1000;
71   while (--iterations && read_codec (status) & (0xFF << 16))
72     rx_buf += (unsigned char)(read_codec (data) >> 24);
73 }
74
75
76 string
77 adac_driver::finish_read ()
78 {
79   write_codec (rx_count, 0);
80   string b = rx_buf;
81   rx_buf = "";
82   return b;
83 }
84
85
86 void
87 adac_driver::begin_write (const string& sample)
88 {
89   tx_buf = sample;
90   tx_buf_ptr = 0;
91
92   write_codec (tx_count, -1);
93 }
94
95
96 void
97 adac_driver::poll_write ()
98 {
99   unsigned iterations = 1000;
100   if (tx_buf_ptr < tx_buf.size())
101     while (--iterations && read_codec (status) & (0xFF << 24))
102       {
103         write_codec (data, tx_buf[tx_buf_ptr] << 24);
104         tx_buf_ptr ++;
105       }
106 }
107
108
109 void
110 adac_driver::finish_write ()
111 {
112   /* wait for codec to flush previous bytes */
113   int pending = 0;
114   do
115     {
116       unsigned s = read_codec (status);
117       pending = (s & (0xFF << 8));
118     } while (pending);
119
120   write_codec (tx_count, 0);
121   tx_buf = "";
122   tx_buf_ptr = 0;
123 }