kennethdevel
2018-7-13 09:43:23

Why is it called car, cdr, cons?


mark.warren
2018-7-13 09:49:51

Ancient history, I believe they are the original assembly command names for ‘contents address register’, ‘contents data register’. Forgotten about cons


mark.warren
2018-7-13 09:50:38

Cons is short for ‘constructs’


mark.warren
2018-7-13 09:52:29

Sorry cdr is ’contents decrement register, more here https://en.wikipedia.org/wiki/CAR_and_CDR and here https://en.wikipedia.org/wiki/Cons


mark.warren
2018-7-13 09:55:04

I could have that slightly wrong but that is the basic idea. Tradition from the old days.


kennethdevel
2018-7-13 10:02:25

Thanks, interesting read.


mark.warren
2018-7-13 10:04:49

No problem


kennethdevel
2018-7-13 10:40:44

Why doesn’t (cons (cons 1 2) (cons 3 4))=>'((1 . 2) . (3 . 4))`?


mark.warren
2018-7-13 11:05:12

Ok, I’ll try to explain but I’m a beginner and I might baffle myself.


mark.warren
2018-7-13 11:05:42

The situation here I think is the difference between a list and a pair.


mark.warren
2018-7-13 11:06:13

A list has an empty list in the last cdr position of the cons cell chain


mark.warren
2018-7-13 11:06:41

(cons 1 ’()) produces a list ’(1)


mark.warren
2018-7-13 11:07:17

A pair has does not have an empty list in the cdr position of the cons cell chain


mark.warren
2018-7-13 11:07:37

(cons 1 2) produces ’(1 . 2)


soegaard2
2018-7-13 11:08:50

@kennethdevel ’((1 . 2) . (3 . 4)) is produced. The default printer just prints it as ’((1 . 2) 3 . 4)


mark.warren
2018-7-13 11:09:37

So what you have done is cons’ed a pair which is displayed as (1 . 2) with another pair, creating a pair with the pair (1 . 2) in the car position and (3 . 4) in the cdr position


kennethdevel
2018-7-13 11:11:27

@soegaard2 Is there a reason why it is printed like that?


mark.warren
2018-7-13 11:11:52

as @soegaard2 has just explained the printer displays a pair as (3 . 4) so ’((1 .2) 3 . 4) is the pair in the car position displayed as a separate pair and the second pair displayed as a normal pair within the outer parentheses


mark.warren
2018-7-13 11:12:55

Because ’((1 . 2) (3 . 4)) would not be a pair but a list of two pairs


soegaard2
2018-7-13 11:13:12

That’s what mwarren explains. A normal list (1 2 3) is short for (1 . (2 . (3 . ()))) so if the cdr is a pair (here the cdr is (2 . (3 . ())) then the printer will print the value as a list.


mark.warren
2018-7-13 11:15:07

Yup


soegaard2
2018-7-13 11:16:04

@kennethdevel If you are using DrRacket, try turning on “Constructor Printing”


soegaard2
2018-7-13 11:16:33

In the menu “Language” choose “Choose Language…”


soegaard2
2018-7-13 11:16:49

The click the button “Show Details”


soegaard2
2018-7-13 11:17:07

Then choose “Constructor” under “Output style”


soegaard2
2018-7-13 11:17:12

Then try your example again.


kennethdevel
2018-7-13 11:30:37

Then I get (cons (cons 1 2) (cons 3 4))


mark.warren
2018-7-13 11:33:15

Yes, that shows you how the cons chain was constructed.


kennethdevel
2018-7-13 11:36:03

I think it is just the printing that confuses me


kennethdevel
2018-7-13 11:36:22

when the 3 . 4 is without ()


kennethdevel
2018-7-13 11:36:52

since (pair? ’((1 . 2) . (3 . 4))) => true


mark.warren
2018-7-13 11:39:00

It’s to do with the differences between how the reader works and how the writer works. It is difficult to explain but (pair? ’((1 . 2) 3 . 4)) is also true


mark.warren
2018-7-13 11:41:44

The writer shows the result of (cons (cons 1 2) (cons 3 4))` as ’((1 . 2) 3 . 4) as it is the standard way of displaying it but there are a number of possible ways of displaying it. The writer just uses one particular way of doing it.


jerome.martin.dev
2018-7-13 11:42:49

By default the writer displays cons chains as lists, so that it’s easier to read and reason about them as list. So it never displays the cdr between parentheses.


jerome.martin.dev
2018-7-13 11:43:17

It just considers the cdr to be “the following part of the list”


jerome.martin.dev
2018-7-13 11:45:46

so anytime you pass a list or a pair to the cdr part of cons: (cons x <here>) it will be considered as the tail of the list, and won’t be displayed as a pair, but will be recursively read until hitting '()


jerome.martin.dev
2018-7-13 11:47:22

so that when you write (cons 1 (cons 2 (cons 3 '()))) it displays as '(1 2 3)


jerome.martin.dev
2018-7-13 11:50:30

so in your example, when you write (cons (cons 1 2) (cons 3 4)) you are telling “make me a list that starts with (cons 1 2) then follows with 3 then finishes with 4


jerome.martin.dev
2018-7-13 11:51:39

which is equivalent to ((1 . 2) 3 . 4)


jerome.martin.dev
2018-7-13 11:53:19

which is an incomplete list, because it lacks the final '()


soegaard2
2018-7-13 11:53:52

@jerome.martin.dev What do you want to run with your 6800 emulator?


jerome.martin.dev
2018-7-13 11:54:34

@soegaard2 I’m building my own computer from a 6800, but I didn’t want to fry the chip when experimenting, so I made an emulator :stuck_out_tongue:


soegaard2
2018-7-13 11:55:02

That sounds as a fun project!


jerome.martin.dev
2018-7-13 11:55:09

yep :slightly_smiling_face:


jerome.martin.dev
2018-7-13 11:56:05

Then when building the emulator, I realized that as I was building something that would permit anyone to specify a custom machine architecture, I was making some kind of “universal emulator”, so the emulator project grew a bit bigger than expected :stuck_out_tongue:


jerome.martin.dev
2018-7-13 11:56:41

I’m focusing on making my own architecture work for now, but I’m planning to add already existing machines


kennethdevel
2018-7-13 11:57:34

@jerome.martin.dev I see, thanks.


kennethdevel
2018-7-13 11:57:44

Why is there a distinction betweens pair and list?


jerome.martin.dev
2018-7-13 11:58:08

I made a DSL to write the MPU part, and a DSL to write the Machine Architecture part. (plus an ASM reader)


jerome.martin.dev
2018-7-13 11:58:27

@kennethdevel a pair is an incomplete list that does not finish with a '()


jerome.martin.dev
2018-7-13 11:58:52

therefore you cannot move recursively through a pair


kennethdevel
2018-7-13 11:58:59

is that good or bad?


jerome.martin.dev
2018-7-13 11:59:33

it’s good for saving data that you know are not going to be something else than pairs, like key-value pairs in a hashtable for example


soegaard2
2018-7-13 12:00:02

Think of pairs as the building blocks of lists. However pairs can be used to build trees as well.


soegaard2
2018-7-13 12:00:17

Lists are used so often that they get special treatment.


soegaard2
2018-7-13 12:00:31

For example they print nicely.


soegaard2
2018-7-13 12:01:14

I know the problem (of projects growing in scope).


soegaard2
2018-7-13 12:01:55

I have a 6502 emulator working - and intended to use it for an NES emulator, but never finished.


jerome.martin.dev
2018-7-13 12:02:08

yeah x)


soegaard2
2018-7-13 12:02:35

It can show the title screen of Donkey Kong - so something is working :slightly_smiling_face:


jerome.martin.dev
2018-7-13 12:02:41

nice!


jerome.martin.dev
2018-7-13 12:03:54

I also have a lot of racket projects running in parallel… There’s so much to do… A website framework, a container manager, and a game engine…


jerome.martin.dev
2018-7-13 12:05:11

Plus blog articles I write everytime I learn something new in racket


jerome.martin.dev
2018-7-13 12:06:25

In the end, nothing gets done :stuck_out_tongue:


soegaard2
2018-7-13 12:06:32

!


jerome.martin.dev
2018-7-13 12:07:40

If only that could be my daily job…



mark.warren
2018-7-13 12:10:06

@kennethdevel Although it’s for lisp this page might help explain. Just replace nil with ’() for Racket https://cs.gmu.edu/~sean/lisp/cons/


jerome.martin.dev
2018-7-13 12:10:40

soegaard2
2018-7-13 12:11:12

@jerome.martin.dev btw - why a 6802 ?


jerome.martin.dev
2018-7-13 12:11:24

that’s what I had on my shelf


soegaard2
2018-7-13 12:11:37

as good as any reason


jerome.martin.dev
2018-7-13 12:11:51

yep :stuck_out_tongue:


jerome.martin.dev
2018-7-13 12:16:09

@soegaard2 How does the (affects) work? It seems to be only a literal


soegaard2
2018-7-13 12:17:19

see line 585


soegaard2
2018-7-13 12:18:02

err - I better read some more


soegaard2
2018-7-13 12:18:50

I don’t think I am using the information anywhere.


jerome.martin.dev
2018-7-13 12:18:58

yeah x)


soegaard2
2018-7-13 12:19:49

I guess I saw the opcode tables and thought it could be useful later.


jerome.martin.dev
2018-7-13 12:21:29

yeah cause I struggled a bit with setting the flags (it might have been the hardest part of making the ALU I think), so I thought you had a cleaner way of doing it than case by case on each op


jerome.martin.dev
2018-7-13 12:22:34

I made some common utility functions though, like (addition!) to set the flags for all additions


soegaard2
2018-7-13 12:23:34

Sharing between opcodes is a good thing - it is pointless to fix a bug in one place, and forget to fix similar bugs in other instructions.


jerome.martin.dev
2018-7-13 12:23:59

yeah


jerome.martin.dev
2018-7-13 12:24:44

one thing I like in my design is that I made a DSL for testing the mpu. It provides rackunit and utility for testing registers and flags



jerome.martin.dev
2018-7-13 12:25:38

cleanest tests I’ve ever written I think x)


soegaard2
2018-7-13 12:26:06

Tests are very important in emulators - it just takes too long to hunt down bugs.


kennethdevel
2018-7-13 12:26:21

How does making an emulator work in racket?


jerome.martin.dev
2018-7-13 12:26:54

@kennethdevel pretty much the same as making an emulator in any language, except you have macros to help you :slightly_smiling_face:


jerome.martin.dev
2018-7-13 12:27:25

but an emulator is really simple to write once you get the basics


kennethdevel
2018-7-13 12:27:40

I am a total newbie, care to explain?


jerome.martin.dev
2018-7-13 12:28:15

you have a big list of operations you take from the datasheet of the processor you want to emulate, then you make a loop that reads a ROM file, and for each byte, executes the corresponding operation


soegaard2
2018-7-13 12:28:54

For a simple computer you have a memory and a cpu.


soegaard2
2018-7-13 12:29:13

A simple model for memory is just a vector of bytes.


soegaard2
2018-7-13 12:29:24

The cpu has a program counter (pc).


soegaard2
2018-7-13 12:29:31

The cpu loops:


soegaard2
2018-7-13 12:29:47
  1. Read the instruction in memory address given by pc.

soegaard2
2018-7-13 12:29:58
  1. Figure out what the instruction does (decode)

soegaard2
2018-7-13 12:30:04
  1. Execute the instructions.

soegaard2
2018-7-13 12:30:20
  1. Maybe increment pc

soegaard2
2018-7-13 12:30:40

So you program is mostly one big loop.


kennethdevel
2018-7-13 12:31:32

and you can do all that in racket, or do you need to go from racket->asm at some point?


jerome.martin.dev
2018-7-13 12:31:40

you don’t


jerome.martin.dev
2018-7-13 12:32:09

but reading and writing asm can be useful when working with custom programs to test your emulator


jerome.martin.dev
2018-7-13 12:33:01

if you just use ROM from real devices (like game cartridges for example), you never have to use asm


jerome.martin.dev
2018-7-13 12:33:28

but if you want to write your own programs, you need to write assembly code and assemble it into binary


jerome.martin.dev
2018-7-13 12:35:05

this was my case, so I made a racket assembler: https://github.com/euhmeuh/virtual-mpu/tree/master/private/asm-lang


kennethdevel
2018-7-13 12:37:31

so if you just download a ROM file you can do everything in racket?


soegaard2
2018-7-13 12:40:10

You can think of a ROM as a snapshot of memory.


soegaard2
2018-7-13 12:40:32

It just determines what’s in memory when your emulator is started.


andreiformiga
2018-7-13 14:55:51

my idea is also to have the CPU emulation be defined by a DSL, it’s not 100% there yet


andreiformiga
2018-7-13 14:56:11

but the DSL is also intended to be easily translated to C and assembly


andreiformiga
2018-7-13 14:56:36

one thing I want to do is create “baremetal” emulators on the raspberry pi, a dedicated emulation machine


andreiformiga
2018-7-13 14:56:46

so there will be a compiler


githree
2018-7-13 15:02:24

Will it be easy to use your emulators for training machine learning algorithms in Racket?


andreiformiga
2018-7-13 15:03:59

it’s possible, but for example my Z80 emulator runs at 2x the normal machine speed, so training can take more time than an emulator that is able to speed up more


githree
2018-7-13 15:04:55

I see


andreiformiga
2018-7-13 15:07:25

translating the emulator to C and using that would make sense


andreiformiga
2018-7-13 15:10:06

@jerome.martin.dev about your fear of frying the chip… a 6802 seems simple enough that recreating one using FPGA shouldn’t be too hard…


andreiformiga
2018-7-13 15:11:06

another project I have in mind is a Racket DSL for hardware… verilog is lame


soegaard2
2018-7-13 15:14:19

+1


githree
2018-7-13 15:16:11

@andreiformiga have you seen Robby Findler’s mini-hdl: https://youtu.be/hFlIl0Zo234?t=35m26s


andreiformiga
2018-7-13 15:16:33

@githree I have not, thanks for the pointer


andreiformiga
2018-7-13 15:21:04

andreiformiga
2018-7-13 20:37:21

@soegaard2 I have a PR to your 6502 readme, change “The 6502 was the star of the 8-bit era” to “The 6502 was one of the stars of the 8-bit era” :slightly_smiling_face:


soegaard2
2018-7-13 20:37:43

well…


andreiformiga
2018-7-13 20:38:56

old school tech holy wars: 6502 vs Z80


soegaard2
2018-7-13 20:39:19

My first computer was an zx81 - it had an z80.


andreiformiga
2018-7-13 20:39:51

here in brazil, the most popular 8-bit computer was the MSX line, most popular 8-bit console was the Sega Master System, so here the Z80 dominated


soegaard2
2018-7-13 20:40:22

How about the drean?


andreiformiga
2018-7-13 20:40:35

the C64 was almost unknown down here


soegaard2
2018-7-13 20:40:48

Surprised to hear that.


soegaard2
2018-7-13 20:41:09

(From Denmark btw)


andreiformiga
2018-7-13 20:41:11

there were clones of the Apple II, ZX81 and ZX Spectrum


soegaard2
2018-7-13 20:41:59

It is kind of strange how different 8-bit systems were popular in different countries.


soegaard2
2018-7-13 20:42:11

I know the msx was popular in the netherlands.


andreiformiga
2018-7-13 20:42:25

yes, also spain


soegaard2
2018-7-13 20:42:27

On the other hand the apple ii never made to denmark.


andreiformiga
2018-7-13 20:45:05

the companies that handled the MSX and the Master System here (both officially licensed) did good jobs on production, support & marketing, so they became dominant


andreiformiga
2018-7-13 20:45:14

though there were plenty of NES clones too…


soegaard2
2018-7-13 20:45:45

I can’t remember the master system. All my friends had c64s. I think one kid had a NES.


andreiformiga
2018-7-13 20:48:31

I remember seeing Amigas here, I was pretty impressed with one I saw in a friend’s house


andreiformiga
2018-7-13 20:49:01

I don’t remember ever hearing about the C64 before the internet


andreiformiga
2018-7-13 20:50:09

@soegaard2 anyway I may base my 6502 core on your code, though I will probably change it to use my DSL


soegaard2
2018-7-13 20:50:21

Feel free to do that.


soegaard2
2018-7-13 20:50:33

I went zx81 -> c64 -> Amiga 500.


andreiformiga
2018-7-13 20:50:49

I am curious about emulating the C64 to try its games


soegaard2
2018-7-13 20:50:52

After the Amiga I couldn’t grasp why anyone would buy a pc :slightly_smiling_face:


andreiformiga
2018-7-13 20:51:11

the amiga was amazing… old PCs were so bad for games


andreiformiga
2018-7-13 20:52:32

unfortunately it was too expensive here, after my MSX broke I had to wait a couple of years to be able to buy a PC, never had an amiga


soegaard2
2018-7-13 20:53:36

I used my Amiga through high school. The word processor didn’t support math, so I had to write formulas with integral signs etc, print the document and them add the missings signs using a pen.


mwb
2018-7-13 21:11:59

@greg In the screenshot you posted a few days ago, is that Emacs? Is it fairly easy to add a REPL along with the logger you have broken out?


greg
2018-7-13 21:19:29

@mwb Yes it is Emacs. The *Racket Logger* buffer is from racket-mode which you can package-install from MELPA (or see source https://github.com/greghendershott/racket-mode )


mwb
2018-7-13 21:21:59

@greg Thanks! I have a lot to learn about Emacs as I’ve primarily used vim but I remote a lot and would love to be able to use something in the terminal.


greg
2018-7-13 21:25:45

DrRacket is really nice, also. That is probably the best/fullest experience, especially when first learning Racket. But some folks like Emacs or Vim to explore many languages, or want to edit many other file formats, etc.


andreiformiga
2018-7-13 22:59:51