OpenCores
URL https://opencores.org/ocsvn/core_arm/core_arm/trunk

Subversion Repositories core_arm

[/] [core_arm/] [trunk/] [soft/] [cdef/] [cdef.el] - Blame information for rev 2

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 tarookumic
;-----------------------------------------------------------------------
2
 
3
(defun int-to-bitstring-rec (n m size)
4
  "Return a bitstring reverse order"
5
  (if (> size 0)
6
      (if (eq (logand n m) 0)
7
          (cons '0 (int-to-bitstring-rec n (lsh m 1) (- size 1)) )
8
          (cons '1 (int-to-bitstring-rec n (lsh m 1) (- size 1)) )
9
      )
10
      '()
11
    )
12
)
13
 
14
(defun int-to-bitstring (n m size)
15
  "Return a bitstring"
16
  (reverse (int-to-bitstring-rec n m size))
17
)
18
 
19
(defun hexchar-to-bitstring (hexchar)
20
  "Convert char to bit sequence"
21
  ( let ((n (downcase hexchar)))
22
    (if (and (>= n ?a) (<= n ?f))
23
      (int-to-bitstring (+ (- n ?a) 10) 1 4 )
24
      (if (and (>= n ?0) (<= n ?9))
25
        (int-to-bitstring (- n ?0) 1 4 )
26
      )
27
    )
28
  )
29
)
30
 
31
(defun hex-to-bitstring (hex)
32
  "Convert hex string into bitstring"
33
  (if (> (length hex) 0)
34
    ( append (hexchar-to-bitstring (elt hex 0)) (hex-to-bitstring (substring hex 1 (length hex))))
35
    '()
36
  )
37
)
38
 
39
;-----------------------------------------------------------------------
40
 
41
(defun mapcar* (function &rest args)
42
  "Apply FUNCTION to successive cars of all ARGS. Return the list of results."
43
  ;; If no list is exhausted,
44
  (if (not (memq 'nil args))
45
      ;; apply function to CARs.
46
      (cons (apply function (mapcar 'car args))
47
            (apply 'mapcar* function
48
                   ;; Recurse for rest of elements.
49
                   (mapcar 'cdr args)))))
50
 
51
 
52
(defun isundef-p (e)
53
  (not (or (eq e 0) (eq e 1))))
54
 
55
(defun and-bit (l)
56
  "And bit using undef values"
57
  (if (and (eq (nth 0 l) '1) (eq (nth 1 l) '1))
58
    '1
59
    (if (or (isundef-p (nth 0 l)) (isundef-p (nth 1 l)))
60
      'U
61
      '0
62
    )
63
  )
64
)
65
 
66
(defun or-bit (l)
67
  "And bit using undef values"
68
  (if (and (eq (nth 0 l) '0) (eq (nth 1 l) '0))
69
    '0
70
    (if (or (isundef-p (nth 0 l)) (isundef-p (nth 1 l)))
71
      'U
72
      '1
73
    )
74
  )
75
)
76
 
77
(defun make-undef-bit (l)
78
  "Undef values on l[1] == 0"
79
  (if (eq (nth 1 l) '1)
80
    (nth 0 l)
81
    'U
82
  )
83
)
84
 
85
(defun make-isundef-bit (b)
86
  "1 if not undef"
87
  (if (isundef-p b)
88
    '1
89
    '0
90
  )
91
)
92
 
93
(defun and-bitstring (a b)
94
  "And bitstring"
95
  (mapcar' 'and-bit (mapcar* 'list a b))
96
)
97
 
98
(defun or-bitstring (a b)
99
  "Or bitstring"
100
  (mapcar 'or-bit (mapcar* 'list a b))
101
)
102
 
103
(defun make-undef-bitstring (a u)
104
  "Set undefined value where u == 0"
105
  (mapcar 'make-undef-bit (mapcar* 'list a u))
106
)
107
 
108
(defun make-set-bitstring (a)
109
  "Make maskestring where not undefined"
110
  (mapcar 'make-isundef-bit a)
111
)
112
 
113
 
114
;-----------------------------------------------------------------------
115
 
116
(defun in-bound-p (start end pos)
117
  (and (>= pos start) (< pos end)))
118
 
119
(defun count-bits (a)
120
  "Count n equal bits from start"
121
  (let ((e (pop a))
122
        (n 1))
123
    (if (eq e '())
124
      (setq n 0)
125
      (while (eq e (nth 0 a))
126
         (pop a)
127
         (setq n (+ n 1))
128
      )
129
    )
130
    `,n
131
  )
132
)
133
 
134
 
135
(defun make-setbit-groups-rec (a)
136
   (if (> (length a) 0)
137
      (let ((n (count-bits a))
138
            (e (nth 0 a)))
139
        (append (make-setbit-groups-rec (nthcdr n a)) `(,(make-list n e)))
140
      )
141
      '()
142
   )
143
)
144
 
145
 
146
(defun make-setbit-groups (a)
147
   "Raise the consecutive 1s"
148
   (reverse (make-setbit-groups-rec a)))
149
 
150
 
151
(defun or-setbit-groups-func (l)
152
  (let ((a (nth 0 l))
153
        (b (nth 1 l))
154
        (al (length (nth 0 l)))
155
        (bl (length (nth 1 l)))
156
        (ae (nth 0 (nth 0 l)))
157
        (be (nth 0 (nth 1 l)))
158
        (oe (logior (nth 0 (nth 0 l)) (nth 0 (nth 1 l))))
159
        )
160
    (insert (number-to-string oe))
161
    ( if (eq al bl)
162
        (
163
         if (> al bl)
164
            ()
165
            ()
166
        )
167
    )
168
    ))
169
 
170
 
171
 
172
(defun rec-copy-list-func (e)
173
  (if (listp e)
174
      (mapcar 'rec-copy-list-func e)
175
   `,e)
176
)
177
 
178
(defun rec-copy-list (l)
179
  "Copy list"
180
  (mapcar 'rec-copy-list-func l)
181
)
182
 
183
(defun cut-copy-list (l start end)
184
  "Copy list and cut range"
185
  (let ((c (rec-copy-list l)))
186
    (if (<= end 0)
187
        (setq c '())
188
        (if (<= end (length c))
189
            (setcdr (nthcdr (- end 1) c) '())
190
        )
191
    )
192
    (nthcdr start c)
193
  )
194
)
195
 
196
;l:        (0  0  0  0  0)
197
;p1:     1     x
198
;p2:     3         |<x
199
;result:  ((0)(1  1)( 0 0))
200
;(split-setbit-copy-list '(0  0  0  0  0) 1 3)
201
 
202
(defun split-setbit-copy-list (l p1 p2 )
203
  "Splits list between [p1,p2]  and fill [p1,p2] with ones"
204
  (let
205
      ((r l)
206
       (size (length l)))
207
    (if (and (in-bound-p 1 size p1)(in-bound-p 1 size p2))  ; split into 3
208
        (
209
          `( ( ,(cut-copy-list l 0 p1) ) ( ,(make-list (- p2 p1) 1) ) ( ,(cut-copy-list l p2 size)))
210
         )
211
        (if (in-bound-p 1 size p1)                          ; split into 2
212
           (
213
            `( ( ,(cut-copy-list l 0 p1) ) ( ,(make-list (- size p1) 1) ) )
214
            )
215
           (if (in-bound-p 1 size p2)                       ; split into 2
216
               (
217
                `( ( ,(make-list p2 1) ) (  ,(cut-copy-list l p2 size) ) )
218
               )
219
               (if (and (< p1 0) (>= p2 size))
220
                   `( ,(make-list size 1) )
221
                   `( ,l);                                       ; do not split
222
               )
223
           )
224
        )
225
    )
226
    ))
227
 
228
 
229
;l:      ((1  1  1)(0  0)(1  1))
230
;off:  2         x
231
;elt: (x x)
232
;result: ((1  1)(1)(1)(0)(1  1))
233
;(or-setbit-groups-into '((1  1  1)(0  0)(1  1)) 2 '(0 0))
234
 
235
(defun or-setbit-groups-into (l off elt)
236
  "Insert splitting range element <elt> into list <l> at offset <off>"
237
  (let ((cur 0)
238
        (start off)
239
        (end (+ off (length elt)))
240
        (v '())
241
        )
242
    (dolist (e (mapcar (function (lambda (x)
243
      (let ((curstart cur)
244
            (curend (+ cur (length x))))
245
        (setq cur curend)
246
        (split-setbit-copy-list x (- start curstart) (- end curstart)
247
            )))) l )
248
     v)
249
      (setq v (append v e))
250
      )
251
))
252
 
253
;a:      ((1  1  1)(0  0)(1  1))
254
;b:      ((0)(1  1  1)(0  0)(1))
255
;result: ((1)(1  1)(1)(0)(1)(1))
256
;(or-setbit-groups '((1  1  1)(0  0)(1  1)) '((0)(1  1  1)(0  0)(1)))
257
 
258
(defun or-setbit-groups (a b)
259
  "Split and or 2 raised setbit groups"
260
  (let ((value a)
261
        (off 0)
262
        )
263
    (dolist (elt b value)
264
      (if (eq (nth 0 elt) 1)
265
          (setq value (or-setbit-groups-into value off elt))
266
      )
267
      (setq off (+ off (length elt)))
268
      )))
269
 
270
 
271
(setq a '(1 1 0 0))
272
(setq b '(1 0 0 0))
273
(setq c '(1 1 0 1))
274
(setq ac (make-setbit-groups a))
275
(setq bc (make-setbit-groups b))
276
(setq cc (make-setbit-groups c))
277
 
278
(setq dc (or-setbit-groups ac bc))
279
(or-setbit-groups dc cc)
280
 
281
(or-setbit-groups-func '((0) (1 1 )))1
282
 
283
 
284
(setq l '(2 3 4))
285
(setcdr l '(4))
286
l
287
 
288
(setq a1 (hex-to-bitstring "ff"))
289
(setq b1 (hex-to-bitstring "f0"))
290
(setq c1 (make-undef-bitstring a b))
291
 
292
(setq a2 (hex-to-bitstring "ff"))
293
(setq b2 (hex-to-bitstring "f8"))
294
(setq c2 (make-undef-bitstring a b))
295
 
296
(setq cc c2)
297
 
298
(setq cu (make-set-bitstring c2))
299
 
300
 
301
 
302
 
303
(setq h (make-hash-table))
304
(puthash 'a a h)
305
(puthash 'b 2 h)
306
(puthash 'c 3 h)
307
 
308
(list (gethash 'c h) (gethash 'a h))
309
 
310
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.