The Novels

Economics 101, a Novel (Rough Draft) -- My first sustained attempt at a novel, two-thirds finished in rough draft, and heading a little too far south.
What would you do if you and your study partner, with whom you had been seriously discussing marriage, suddenly found yourselves all alone together on a desert island? Study economics?
Sociology 500, a Romance (Second Draft) -- The first book in the Economics 101 Trilogy.(On hold.)
Karel and Dan, former American football teammates and now graduate students, meet fellow graduate students Kristie and Bobbie, and the four form a steady study group.

Featured Post

Sociology 500, a Romance, ch 1 pt 1 -- Introducing Bobbie

TOC Well, let's meet Roberta Whitmer. Bobbie entered the anthropology department office and looked around. Near the receptionis...

Sunday, July 26, 2020

Backup: 33209: Straits -- Exercises, Exercises

Backup of https://joelrees-novels.blogspot.com/2020/07/33209-straits-exercises-exercises.html.


Chapter 13.4 Straits -- Keyboard Decoding

Chapter 13.5: Straits -- Enclosures?


The doorbell rang as I was changing into gym shorts and a tee shirt for my morning exercises.

Giselle called out, "I think you should get it, Joe."

Puzzled, I pulled my tee on and carried my gym shoes into the living room.

Julia was waiting at the screen door in exercise gear when I rounded the hall doorway into the living room.

"Oh. So that's why you wanted to drive today." I gave her a cheesy grin and went to the door, lifting the screen door latch so she could come in. "You wanted to come early and see what I do for exercise."

"Well, more to join you than to watch." She loosened her shoelaces and left her shoes inside the door as she came in.

I set my shoes by hers. "I could have gone to your house early."

"And exercise with my parents watching? No way. Not the first time we exercise together, anyway. How do you usually start?"

"Warmups and stretches. The carpet in here is easy on the bones, but it's also easy to slip on. The floor in the den is hard, but you don't slide unless you're wearing socks."

"Barefoot it is, then."

We both left our socks with our shoes, saying good morning to my mom on our way through the kitchen to the den.

"You two focus on your exercises and I'll fix breakfast this morning."

Mom must have also been in on the plan.

We faced each other in the middle of the den. "And?" Julia looked at me expectantly.

"Uhm, I usually start with plié to warm up. Other than that, it's pretty random, and I'm open to suggestions."

"From ballet?"

"I learned a little from Louise and Giselle, but it's a hodge-podge mix. I wanted to take a class in modern dance this semester, but they told me I already had more than the usual limit of credit hours."

She laughed. "Like I say, you work too hard."

I had to chuckle.

I stood with my feet and arms in a rough approximation of first and second positions, respectively, and Julia mirrored me.

"One. Two." We did shallow knee bends. "Three, two, three, four." Once deeper, and back to standing. Four, two, three, four." Up to relevé and back. And we repeated four times: plie, plié, grand plié, relevé.

Julia broke into jumping jacks. "One, two, three, four" I caught up and mirrored her. "Two, two, three, four. Three, two, three, four. Four, two, three, four." She stopped. "That's base four, isn't it?"

I stopped and nodded.

"But it ends at four, not three." She wrinkled her forehead.

"Because it starts at one. Using different symbols doesn't really change the ring. It would be more the symbolic arithmetic base four if we started at zero, of course."

"But no one does that when exercising."

"Most people forget math when they exercise."

She chuckled. "We've stalled. One-zero --" She started the jumping jacks again. "-- one-one, one-two, one-three."

"Trying to say all that breaks your concentration, doesn't it?" I joined in and we continued through three-three and on.

"One-one-zero, one, two, three. One-two-zero, one, two, three. One-three-zero, one, two three." She shook her head and stopped again. "Can't keep it going past three-three. Uhm, that's fifteen, right?"

I stopped, too. "Right. Do it in hex?

"Zero, one, two, three, four, five, six, seven, eight, nine, aye, bee, cee, dee, ee, eff."

Somehow, we kept up the exercises and the counting in hexadecimal, in spite of the laughter, up to 100sixteen.

"Exercising with you gets my heart beating."

Julia rolled her eyes and slapped me gently over the head. "That was a lot of jumping jacks. What next?"

"Push-ups to relax?"

"Does not sound relaxing."

"Exercising different muscles."

Julia pursed her lips and got down into push-ups position, and I followed, and we did seven together.

"My limit," she said.

"Mine, too."

"Don't be nice."

"Seriously. I can't even do more then seven at once from the knees." This was true. I ididn't learn how to do push-ups until a modern dance teacher explained visualizing muscle use to the class once. "Sit-ups?"

We matched each other for twenty sit-ups, or 1Asixteen.

"Whew." She sat up. "We should take modern dance together."

I wonder if I'll have time for any summer classes, with the internships."
 
"Next fall."

"I thought you were moving on to Texas Christian. Up for a run?" 

"Maybe. Mom thinks I should postpone the associate's degree."

"You need to do what you need to do."

She gave me a perplexed look. "That's right. And I think I agree with my mom."

"Oh."

"Where do you usually run?"

"Lots of places. I learned how to pace myself and go for distance on my mission."

"Sounds scary."

"Sometimes I run past your house."

"And you don't stop in?"

"I think I will, next time."

"How long does it take?"

"About fifteen minutes, round trip."

"I think I can handle that. Better stretch first."

We both started doing stretches.

"I guess, if postponing your associate's is what you need to do, I'm not going to complain."

She turned and gave me an air kiss. I grinned and returned it.

"Can you study your Japanese at UTPB?"

"They don't have any courses to take, just like OC. I'd be the teacher."

"I've been thinking about finishing my bachelor's degree at UTPB."

"Me, too. I think I can do the Japanese by correspondence through BYU, if UT doesn't want to help me with it."

Our conversation lagged for a couple of minutes while we focused on the stretches. Then we stood, and she gave me a quick real kiss before we went in to the kitchen to tell my mom where we were going.

"Have fun. Be sure to get back in time to get cleaned up before classes."

We headed out, crossing 42nd at Dixie Boulevard, passing the shopping center on our right on our way to the park, heading east from there past her church, and stopping to surprise her mom at her house. From there we headed back south, crossing 42nd at the Lindale intersection where Lindale turns into 38th Street, then following 38th back home.

We stretched some more and cooled down in the den.

"I think you can borrow the shower on my parents' side of the house to wash off the swe-- uh, glow."

"South side bathroom's ready," my mom informed us from the kitchen.

I rolled my eyes. "Glad somebody here is good at planning."

Julia laughed and leaned over to give me another kiss. "Planning is something I'm good at."

"You're good at many things."

She gave me a smirk.

"Maybe we should agree before the trip to Austin, not to kiss during the trip."

Her smirk changed to a look of hurt, then a pout. Then she sighed. "You're probably right. Are you sure about your temples being hard to elope to?"

"Very hard. We both have to be members in good standing, and there's a year wait for new members."

"A what?"

"Temples are serious business. Promises to God. We want the members to be sure that they are willing to follow through on the promises."

"What are the promises?"

"Can't be specific, but basically to do our best to follow a godly walk and live a Christian life."

"That shouldn't be so hard." But she stopped and thought. "At least, it wouldn't be hard for me."

"The promises are easy to misunderstand without preparation, and the only ones who can really know if you're prepared are God and you, yoursefl. So I guess it seems better to just have everyone wait at least a year."

"Hmm. And I'm not really a member of your church yet, anyway. Darn."

I chuckled, and she sighed, then joined me in chuckling.

"I need to ... wash off the glow."

I shook my head. "Anything I say about that, I'm dead in the water."

She laughed.

Mom made sure Julia had what she needed for her shower on the south side of the house, and I took a quick shower in the other bathroom, on Giselle's and my side, and we packed our stuff in her car and headed to school.

Chapter 13.6: Enclosures?

[Backed up at https://joel-rees-economics.blogspot.com/2020/07/bk-33209-straits-exercises-exercises.html.]


Friday, July 24, 2020

Notes: 33209: Straits -- Keyboard Decoding, Exercises

Notes for https://joel-rees-economics.blogspot.com/2020/07/bk-33209-straits-keyboard-decoding.html and https://joel-rees-economics.blogspot.com/2020/07/bk-33209-straits-exercises-exercises.html.
May contain spoilers, both real and false.


Chapter 13.3: Straits --Display

Chapter 13.4: Straits -- Keyboard Decoding

"Much improvement last night."

I chuckled and gave Julia's mom an air kiss on the cheek as I ducked in the door. "Somebody was peeking through the blinds?"

She just answered with a smile that might have been a little smug.

Julia came into the living room from the hallway, carrying her backpack on a shoulder. "I threatened to do all our kissing somewhere else, and Mom didn't even bat an eyelash," she complained.

"You two belong together. You can kiss anywhere." Mrs, Cisneros gave her daughter a quick hug and a kiss and sent us off with a "Have a good time studying together today."

I held the passenger-side front door of the Colt open and Julia climbed in.

She leaned around and put her backpack in the back seat as I went around and climbed in the driver's seat. "How about if I drive tomorrow?"

"Your folks don't need the car?"

I started the engine and the radio came on.

"Yeah. Well, I want to drive."

"Sure. I'll be waiting for you."

The radio played in the background as we talked about the day's schedule and other things.

"I don't like this song," she said as the radio played the chorus of Mary MacGregor's hit rendition of "Torn between Two Lovers".

"Mmmyeah. It does kind of get stuck in the head, and the lyrics aren't all that great."

Julia was silent for a moment, then asked, "Do you think it would be breaking rules to love two people?"

I had to think for a minute. "My automatic reaction is that's not the right kind of love if it's breaking any real rules."

Julia reached over to the radio and changed the station. The station she picked was playing Boston's "More Than a Feeling", and we listened to it in silence the rest of the way to school, holding hands on the gearshift lever.

*****

"Has anybody noticed that 24 bits of parallel I/O aren't enough to do both the keyboard and the displays?" I was sitting down in front of the computer when I asked this, and several hands raised.

"Two separate 68705s?" Bob asked.

I nodded. "The 24 bit I/O varient we are using, yeah, that would work."

Kyle laughed. "So we didn't need to bother with the timer and interrupts."

"We got 'em," Tim replied. "Might as well use 'em."

"Yeah," I agreed. "Especially in this little club or ours. But what else can we do besides using two separate controllers? Two controllers would have the disadvantage of needing either another port on the mainboard CPU, or some way of sharing a single port."

Jennifer and Bob discussed between themselves for a moment, then Jennifer turned around and said, "How many bits of I/O do we need?"

"Excellent question. Let's get our diagrams out, add the CPU interface, and count. Can you guys draw that on the board?"

The two of them went to the chalkboard, and shortly they had this up:


They stood back and looked at it with doubt. Then Jennifer said, "But we need some way for the mainboard CPU to tell the controller whether to read the keyboard or output to the display."

"Add a control line to the main CPU port for direction, maybe?" Bob suggested, and Jennifer did so.


"Either a control line, or we need to have some fancy tri-stating buffering and signaling protocol on the interface. Or, if we were talking about a new design instead of the Micro Chroma 68, we could trade the parallel interface with the main CPU for a serial interface."

Carlos asked, "Which are you going to use?"

"I'll ask Julia later. Before we get back to figuring out how to debounce the keyboard, what if this weren't the Micro Chroma 68? Could we use some other interface for the keyboard/status display controller?"

Chuck responded first. "Serial should work, right?"

"Does the 6805 have a serial port?"

"No, but can't we use a bit from the parallel port for it like the Color Computer does?"

"We can, indeed. Thank you. So, back to the debounce -- can we use the same timer setup that we're using for the display to sample the keyboard matrix and debounce it?"

----------------



I was going to ask first off if anyone had noticed that 32 pins of parallel I/O wasn't really enough, but, when Julia and I arrived, a group was gathered around Winston, where he was testing his keyboard and seven-segment displays. When he held a key down, it would interfere with the display.

He looked up at me. "I don't think we have enough output lines."

"Yeah, ...," I hedged.

Bob looked up, too. "I'm thinking we could add two 'LS138s and control them from port C to do the scanning, one for the displays and one for the keyboard matrix."

"Oh. That might work." I nodded absently. "Can we get a look at the problem before we go looking for solutions?"

"It's a bug." Winston half-asserted, half-asked.

"Oh, yeah. I was planning on looking at it first thing today when everyone comes. Can you do the explanation, Winston?"

"Why me?" he complained, half-joking, half-resigned, as he picked up his schematics and went to the chalkboard. He started by putting up the working diagram from the day before while we waited for more of the group to come.

When he was done, and most of our group was there, he looked at me and I nodded to Doctor Brown, and Doctor Brown said, "Any time you're ready."

Winston went back to his lab table and picked up his keyboard and controller, and announced, "Ground control, we have a problem."

Several of us chuckled as Winston proceeded to demonstrate how holding certain keys down would change a number on the display to something else.

"That's cool!" Carlos enthused.

"Solution one," Doctor Brown intoned. "It's not a bug, it's a feature."

Everyone laughed except Winston, who rolled his eyes before grinning lopsidedly. "That is one way of looking at it," he grudgingly admitted.

"Yeah," Doctor Brown grinned, "but I guess let's not use that solution this time."

"So, why does this happen?" I asked. "Show us what your circuit looks like."

Winston erased and redrew part of the circuit:


"So, we could ask Winston why he added another buffer, or we could ask everyone if we all understand why. Suzanne, can you tell us why?"

She hesitated, than suggested, "If we don't have that, how does the computer know what key the controller decoded?"

"Exactly. Thanks. So, Winston, now what's the problem?"

"We don't have enough bits of I/O, and I have used one of the buffers for the displays as one of the buffers for the keyboard matrix." He erased and redrew another part of the diagram to show what he had.


"I was hoping that the scan could be done quickly enough to not interfere, but just holding the keys down messes things up."

"So, Bob, you had a suggestion, right?"

"Jennifer thinks we should be able to do it without any additional parts, but I think we need two 74LS138s."

"Can you diagram that?"

"Well, first, I think we need another bit for the main CPU interface."

"Why's that?"

Jennifer replied, "Main CPU has to be able to tell the controller what it's supposed to do, so it needs at least one bit of control."

Bob and Jennifer worked quickly to put the following diagram up:


"Hmm. Let's keep that in mind. So, what about the 'LS138 one-of-eight demultiplexors?"

Bob modified parts of the diagram, to add the two demultiplexors:


"This would allow both the keyboard and the display to be accessed at the same time," Bob explained. "If I shut down the displays for a hundred microseconds or so, I can share the select lines, and save a couple of port bits."

"Okay. Now I think Jennifer is right that, with a bit of really careful timing, and a bit of care in how we set up the ports for the matrix and displays, we could reduce the interference so that you could hardly tell that holding a key down is causing stray LEDs to light. But it would be kind of sensitive to resistor values, and cheap parts might not work."

Jennifer looked a little put-out.

"I'm not saying she shouldn't do it, just that that there are trade-offs, and I'm not prepared to dig into that right now. Back to the 'LS138s, how do they affect our code? Bob and Jennifer, let someone else answer."

Bob and Jennifer both nodded in agreement.

Javier finally spoke up. "Instead of shifting the matrix strobe and the display select on their ports, we count them?"

"Good. What does the counting look like?"

Everyone thought for a bit, and then Javier answered: "Counting for one of those can be just an INC instruction, but the other is going to be adding sixteen or something, depending on the bits used, right?"

"Exactly. So we'll look at that, but anyone have another way to do this?"

Mike said, "I'm planning on sharing a port between the keyboard and the displays, but I'll use a single eight-bit buffer to disable the keyboard when I'm using the displays. I'll use the extra bit from the display select port to shut off the keyboard matrix except when I'm reading it."

"How about the control line for the main CPU?"

"The Micro Chroma 68 keyboard interface only uses seven bits of data, so that leaves one bit that can be used as a strobe or control signal. I think that'll be enough." Mike stood up to a blank panel and sketched out his idea.


"Very good. Now we have at least a couple of different approaches that might work."

Larry asked, "Would it work to use two 6805s?"

"What do you think?"

"I guess we'd need a way for the main CPU to talk to the second controller."

"Sure. And that may have some advantages, as well."

Kyle laughed. "So we wouldn't need to bother with the timer and interrupts."

"We got 'em," Tim replied. "Might as well use 'em."
Terry asked, "How about using a serial port for talking to the main CPU?"
"The Micro Chroma 68 design is looking for the keyboard on a parallel port," I explained. "But if we modify the design and the monitor ROM, we should be able to get a serial interface to work, as well."

Chuck objected, "The 6805 doesn't have a serial port."

"True."

After a moment of thought, he said, "But I guess a single bit of a parallel port could work, the way it's done on the Color Computer."

"Bit-banging. We could put the 6805's bit operators to good use there, I think."

Carlos asked, "Which are you going to use?"

"I'll ask Julia later."

Several of the group chuckled.

"Because it's her keyboard, not mine."

Lupe raised a question -- "Does the Micro Chroma 68 use the status display?"

"Not with the current software, but, again, we can modify the software to use it."

"Then maybe we don't really want the display on the keyboard controllers we're building now?"

"That is definitely a design option," I concurred. "Or, if you want the display but want to do it another way, you could add a 6821 to the mainboard and have the mainboard CPU control it directly."

Tanya complained, "So, basically, for the last three days, we've been on a wild goose chase."

"Exploring options," I reinterpreted. "And learning how to use a timer interrupt, so we can use timed samples instead of riding the port to debounce the keyboard matrix. And looking deeper into what we might want to build."

Larry said, "Okay. Can we get back to debouncing the keyboard, now? Can we use the same timer setup that we're using for the display to sample the keyboard matrix and debounce it?"

"Explain."

"The whole point of the debounce is to be sure we read one keypress as one keypress? We could remember which key we saw in a variable, and check it on the next timer interrupt?"

"How long do we wait to accept a keypress?"

Julia recited, "300 words per minute is five words per second. At six letters per word, that's thirty keys pressed a second, or one thirtieth of a second."

Terry added, "That's about thirty-three milliseconds, so it's about thirty-three interrupts."

Freddie asked, "Can a key bounce take more than a millisecond to settle?"

Suzanne suggested, "We can count interrupts to time a keypress. Maybe sixteen interrupts is good to call it debounced."

I added, "We can experiment with that, and we might even want to add keyboard repeat functions based on counting timer interrupts."



Discussion of multiple simultaneous keys pressed.


State is sparse matrix -- matrix algebra

"Good, but let's think about it a bit further. What if we want to see keys that repeat?"

"We'd need to time it."

Suzanne suggested, "We could count interrupts as a way to time it?"

"Yep. Next question, what if we press one key, then another before we release the first? Can we figure out a way to let the keyboard key rollover, so to speak, to the next key?"

"Have some sort of memory buffer?" Terry suggested.

---------------

Then Carlos asked, "Okay, we've recorded all this state, now what?"

"How do we check to see if something changed?"

From there we talked about using the interrupt counter to tell when we needed to check whether the state had changed, and the about using bit masking and bit counting to tell which keys had been pressed and released, and so forth.

And somebody pointed out that the state table naturally kept track of key rollover, so we talked about adding a type-ahead buffer.

I had to leave to deliver newspapers, and, by the time I got back, Winston, Jeff, and Mark had their keyboards working with their Micro Chroma 68+6801s. Bob, Jennifer, Mike, also had their keyboards working, and those who had theirs working were going around helping others.

Suzanne had joined Julia, so I sat down with them and we got both of their keyboards working and interfaced with their Micro Chroma 68+6801s before time to shut down.

*****

"I need to make a stop in the library." I helped Julia wrap her computer in aluminum foil for protection.

"Can I come with?"

"Well, of course."

*****

"Intel 8086? What do you need that for?"

"Ammunition."

*****

After studying and singing, Julia played with her Micro Chroma 68+6801 while I studied the i8086/8088 manual and the TMS 9900 and 9940 manuals.

When I took her home and we shared a quick kiss on her doorstep, her mother opened the door and cleared her throat.

"Mom, not tonight. I've been studying stuff that is completely foreign to me. Joe calls it playing, but I'm tired."

"I have to admit to being a bit tired myself," I added. "Julia's keyboard is now talking to her computer. Tomorrow's going to be another long day, I think."

"Speaking of her computer," Mr. Cisneros spoke up from the couch, "I think she needs a box to put it in."

"True."

I went in and sat down, and we discussed plans for an enclosure for her computer. We talked about a box like mine, and we also talked about putting her keyboard in a separate enclosure so she move it around, and several other options, sketching up some plans, but making no decisions.



---------------

Lupe raised a question -- "Does the Micro Chroma 68 use the display?"

"Not with the current software, but we might ft ."

"Then maybe we don't really want the display on the keyboard controllers we're building now?"

"That is definitely a design option," I concurred. "Or, another way to add a display, you could add a 6821 to the mainboard and have the mainboard CPU control it directly."

Tanya complained, "So, basically, for the last three days, we've been on a wild goose chase."

"Exploring options," I reinterpreted. "And learning how to use a timer interrupt, so you can use the timer interrupt method of decoding the keyboard matrix. Or taking a side-tour to start designing a trainer."

Larry said, "Okay, let's get back to debouncing the keyboard. I guess we can use the same timer setup that we're using for the display to sample the keyboard matrix and debounce it?"

"Explain."

"The whole point of the debounce is to be sure we read one keypress as one keypress? We could remember which key we saw in a variable, and check it on the next timer interrupt?"

"Good, but let's think about it a bit further. What if we want to see keys that repeat?"

"We'd need to time it."

Suzanne suggested, "We could count interrupts as a way to time it?"

"Yep. Next question, what if we press one key, then another before we release the first? Can we figure out a way to let the keyboard key rollover, so to speak, to the next key?"

"Have some sort of memory buffer?" Terry suggested.




----------------

"In my ideal world, people could be lovers without having, you know, sexual relations."

"Do you think it would make it alright, to be torn between two uhm, platonic lovers? I don't think that even makes sense as a question."

"Heh. I'm not a fan of the song, either, but, like a jingle for fast food, it gets stuck in the brain."


picking julia up in the morning, talk about exercise, julia says she will drive Wednesday (but does not say reason is to to arrive early and exercise with Joe)

in lab, discussing how to deal with needing 8 more port lines because of seven segment display,introduce ls138


The doorbell rang as I was changing into gym shorts and a tee shirt for my morning exercises.

Giselle called out, "I think you should get it, Joe."

Puzzled, I pulled my tee on and carried my gym shoes into the living room.

Julia was waiting at the screen door in exercise gear when I rounded the hall doorway into the living room.

"Oh. So that's why you wanted to drive today. You wanted to come early and see what I do for exercise?" I gave her a cheesy grin and went to the door, ligting the screen door latch so she could come in.

"Well, more to join you than to watch." She loosened her shoelaces and left her shoes inside the door as she came in.

I set my shoes by hers. "I could have gone to your house early."

"And exercise with my parents watching? No way. Not the first time we exercise together. How do you usually start?"

"Warmups and stretches. The carpet in here is easy on the bones, but can slide about underfoot. The floor in the den is hard, but it doesn't slide you unless you're wearing socks."

"Barefoot it is, then."

We both left our socks on our shoes, saying good morning to my mom on our way through the kitchen to the den.

"You two focus on your exercises and I'll fix breakfast this morning."

Mom must have also been in on the plan.

We faced each other in the middle of the den. "And?" Julia looked at me expectantly.

"Uhm, I usually start with plié to warm up. Other than that, it's pretty random, and I'm open to suggestions."

"Ballet?"

"I learned a little from Louise and Giselle. I wanted to take a class in modern dance this semester, but they told me I already had more than the usual limit of credit hours."

She laughed. "You work too hard. I've picked up some things from creative dance classes."

For about ten minutes, we shared ideas about warmups and stretches.

I showed her the first three minutes of NHK rajio taisō, the Japanese public broadcasting system's radio (and TV) exercise routine. (You can probably find videos of people doing it, by searching for "NHK Rajio Taisō".)

"The hands and legs are opposite of plié."

"Yeah. In ballet's Plié, the beat of the hands and legs is in opposition, but in rajio taisō, they start together and beat in the same direction."

"So you learned all this dance from Louise?"

"And Giselle. I wanted to take a class in modern dance this term, but there was no time for it."

"Let's take it together."

"I wonder if I'll have time for summer classes, with the internships. I thought you were moving on to Texas Christian."

"Mom thinks I should postpone the associate's degree."

"You need to do what you need to do."

She didn't look happy with that thought.

"Of course, if that happens to be sticking around and doing undergraduate work at UTPB with me,  that might work, too."

"Can you study your Japanese at UTPB?"

"Nope. Push-ups and sit-ups?"

"Sure. What'll you do about Japanese?"

"I should probably talk to somebody at UT or BYU about correspondence work."

 Our conversation lagged for a couple of minutes while we focused on the exercises.

"Up for a run?"

"Sure."

"Want to run past your house? I think we can make it in ten minutes. I think you can borrow the shower on my parents' side of the house to wash the swe-- glow off."

She laughed. "That might make my mom's day. And I already checked with you mom on the shower."

"Glad somebody here is good at planning."

We told my mom we'd be back in ten and headed out the front door, running north to cross 42nd at Dixie Boulevard, then past the shopping center to run along the south edge of the park. We rounded her block, stopping at her house to say hi to her mom, then back around her church, following Lindale back to the intersection with 42nd, where Lindale turns into 38th, then followed 38th home.

Mom made sure Julia had what she needed for her shower, and I took a quick shower in the bath on Giselle's and my side of the house, and we packed our stuff in her car and headed to school.




Mention of the trip to the surplus store for disk drives, everyone plans to go, plans for picnic?
Framework makes debouncing easy
Having to use the anodes port for the column strobes? Add one-of-eight selects instead?
Using the interrupt input for system request



****

"We want to be able to have the CPU do other things besides display numbers, so we're going to use the timer built into the 6805 to count instead of having the CPU count, and we'll have the timer interrupt the program to actually display the numbers."

"Is this only for displaying numbers?" Tanya asked in a slightly petulant tone.

"Well, the numbers might mean things. For instance, if something goes wrong during boot, we could put an error code and an address on the display, to give clues about the error state. Or we might say that a blank display is an all-operational status."

I can tell your eyes are glazing over now, so, rather than walk you through it with us I'll just note that we read in the manual about the timer, and I walked them through initialization, using the pre-scaler to set the granularity, calculating the value to initialize the timer and pre-scaler to interrupt the CPU once every millisecond, and constructing the interupt routine to display the digits one place at a time.

Someday, somewhere in my blogs, I'll show the complete keyboard debounce and status display routine, complete with display suppression and other features that we evenutally added. Suffice it to say that we had Julia's status display functional at a usable level on Monday before I went home to deliver newspapers, and that about half of the group had theirs functional by the time I got back.


Tuesday I explain that we need more computers running, so Julia's is not just Julia's. Then we forge ahead into the keyboard decoding.

Wednesday, while students are catching up, Julia, Mark, Jeff, Winston, and Suzanne dig in and get their computers ready for RAM, ROM, burners, disk controllers, etc., while I study the Forth manual and the TMS 9940 datasheets.



Which is what we did. Again, Doctor Brown let them work while I was delivering my newspapers, and Julia, Mark, and Jeff helped those who needed help with the tools on the Micro Chroma 68.

About half of the group had their display routines working well by the time I returned to pack things up.

From Monday to Wednesday, in lab, I let the others work while I helped Julia program her keyboard controller. Someone was watching over our shoulders as we worked, and the structure of the code made the rounds -- the interrupt handlers, the state tables for the keyboard matrix, the translation matrix for the LEDs, etc.

Bob and Jennifer pretty much got it worked out for themselves, and we compared notes.

Mark, Jeff, and Mike picked up what we were doing, and also had their keyboard controllers working shortly after Bob, Jennifer, and Julia and I had ours working.




It's tempting to go into great detail on the keyboard decoding and the status display. Maybe, one day, I'll write a book on how to do it with parts you can buy now. 6805s are kind of hard to get now. (It would fill an entire book, yes.)

I'll note that I walked the group through several ways of keeping keyboard state for use in debouncing. Some of the students understood when I






Pascal, book of Ether, keyboard with Julia
Ed Snyder's black stealth keyboard

monday
Julia and I work out the keyboard and seven-segment status display
class also progressing, work on 68705 trainer monitor
home evening with Julia's family and missionaries, second discussion,
recital practice every evening all week

tuesday, wiring Julia's with memory switches, DMA controller, ROMs, full RAM
continued work on trainer monitor
communicating with TSC about drivers
(evening?)

wednesday,
Julia's EPROM/MCU burners, my daughterboard starts
class continues with trainer monitor, etc.
her scripture study group

thursday,
diagramming,
first peek at forth
class completes trainer monitor.
our institute

friday,
trip to austin
meeting with motorola, Julia asked to join
discussion of
-- floppy controllers
-- 2801 address spaces, I/O based memory banking, call stack cache
-- RISC concepts
-- 2809 similar to 2801
-- 31609 with 32 bit addressing
-- 38000 with stack caching

saturday
Julia's disk drives

(Nos) besaremos cuando y como queramos.
cuándo y cómo

Trip back, too much heavy kissing, discussion of information sharing?
Managers and bishops who don't know what to do with information that comes up.

(In notes for bringing up flex:)

Denny called about the time we were wrapping up, and Julia got on the extension in Dad's study to say hi. It seemed it would be appropriate to make a run to Austin the next weekend, and to leave immediately after our last morning classes so I could visit with some of the management at Motorola Friday evening.

"You, know," Julia said, "if I went with, maybe I could look for floppy disk drives at that surplus shop on Saturday before coming back."

Denny checked with Denise, and they decided Julia could sleep on the couch in the living room and I could sleep on the floor in a sleeping bag in the boys' room. Julia's parents said they thought it would be okay, and Mom and Dad suggested we pray about it.

Which we did. No one felt inspired to object, 

Julia suggested she go with me, so she could go looking for her own disk drives.


Talk about duty cycle and mention DtoA as a way to adjust brightness.


Julia returned to working on her keyboard/trainer, while I dug back into my test routines. 


Watching the students work on the keyboard and keypad matrix decoding,  became clear to me that, while the bit I/O instructions might be useful for certain singular, non-generalized kinds of code, a keyboard or keypad matrix contained a lot of repeated elements. Ultimately, shifts and logical instructions would provide the more general scalable solution.

I stood up and demonstrated the concept of a state array for the matrix, then suggested we put it on a back burner and look at the seven-segment LED displays. This time, the bit I/O instructions were ignored, and several groups formed around defining an array of the segment patterns for each integer. Some of the students wanted to turn the translation array upside down for some reason, but I didn't stop them.

Suzanne's and Winston's and Winston's mainboards were both up enough to display the TV-BUG prompt by this point, and the four of them joined the rest on the keyboard/trainers.

It's tempting to go into great detail on the keyboard decoding and the status display. Maybe, one day, I'll write a book on how to do it with parts you can buy now. 6805s are kind of hard to get now. (It would fill an entire book, yes.)

I'll note that I walked the group through several ways of keeping keyboard state for use in debouncing. Some of the students understood when I


Since Flex was running on my Micro Chroma 68, I was able to load the cross-assembler, and we had several students burn code into their 68705s for testing before time to shut down.

Mike kept trying to work out how to set the keyboard state up as an array, but he couldn't seem to quite pin it down.

I shook my head, and she led me out the back door. There was a large tree with a tire swing

I guess there were no surprises on Sunday, unless you would consider it surprising that Pat and George attended Julia's congregation again. They slipped out before we could talk with them. We didn't see Mike.

Or you might consider it surprising that Julia and I joined the choir in my ward, preparing for Easter Sunday. No?

I suppose it would be no surprise that Julia insisted on calling Denny and Denise to ask if there were room for her to stay overnight Friday. With a bit of discussion, it was determined that she could have the couch in their living room, and I would roll a sleeping bag out on the floor of the boys room, and both our parents approved that plan.

That Sunday was about as one might expect, I think. In the morning, I visited Julia's congregation. Pat and George came and sat in the back. Mike slipped in before the sermon started, and all three slipped out before we had a chance to talk with them.

In the afternoon, Julia visited mine. This time, she joined our choir practice in between meetings and young adult family home evening.
I told her about needing to go to Austin before her meetings, and by the time we were finished with mine, she had decided she was going with me.


"Floppies multiplying like rabbits," I grumbled.

She laughed. 




tendency to idealize/idolize foreign



Chapter 13.5: what?

[Backed up at .]


Sunday, July 12, 2020

Backup: 33209: Straits -- Keyboard Decoding

Backup of https://joelrees-novels.blogspot.com/2020/07/33209-straits-keyboard-decoding.html.


Chapter 13.3: Straits -- Status Display

Chapter 13.4: Straits -- Keyboard Decoding


"Much improved last night." Julia's mom gave me an approving smile as she met me at their front door.

I grinned lopsidedly and bussed her cheek as I ducked in. "Somebody must have been peeking through the blinds?"

The smile she gave me in answer might have been a little smug.

Julia came into the living room carrying her backpack on a shoulder. "I threatened to do all our kissing somewhere else, and Mom didn't even bat an eyelash," she complained.

"You two belong together. You can kiss anywhere." Mrs. Cisneros gave her daughter a quick hug and a kiss and sent us off with a "Have a good time studying together today."

I held the passenger-side front door of the Colt open and Julia climbed in.

She leaned her seat back and put her backpack in the back seat as I walked around and climbed in the driver's seat.

"How about if I drive tomorrow?"

"Your folks won't need your car?"

The radio came on as I started the engine.

"I can usually, I just want to drive. tomorrow."

"Okay. I'll be waiting for you."

DJ talk and music played in the background as I drove and we talked about the day's schedule and other things.

"I don't particularly like this song," Julia said as the radio played the chorus of Mary MacGregor's hit rendition of "Torn between Two Lovers".

"Mmmyeah. It does kind of get stuck in the head, and the lyrics aren't all that great"

Julia was silent for a moment, then asked, "Do you think it would be breaking rules to love two people?"

I thought for a minute. "My automatic reaction is that's not the right kind of love if it's breaking any real rules."

"Platonic okay, Plutonic no." She gave me a shy-sly grin as she reached over to the radio to change stations.

"Heh."

The station she picked was playing Boston's "More Than a Feeling", and we listened to it in silence for a bit, holding hands on the gearshift lever.

"Hey. I'm going to make everyone rewire their keyboard controllers today. I hope they won't be too mad."

"Why's that?"

"Not enough port bits. Need to add a buffer and share one port between the keyboard and display, to free up a port to talk with the main CPU."

"I might be a little put out, too."

She tickled my palm, and I missed a downshift, letting the engine lug for a moment while I returned the tickle.

*****

In the lab after classes, I was going to ask first off if anyone in our group had ideas about how to shorten the interrupt handling routines for the displays, and ask after that if anyone had noticed that 32 pins of parallel I/O wasn't really enough. But, when Julia and I arrived, a group was gathered around Winston. He was testing his keyboard and seven-segment displays, and when he held a key down, it would interfere with the display, causing it to show stray segments.

He looked up at me. "I don't think we have enough output lines."

"Yeah, ...," I hedged.

Bob had a suggestion. "I'm thinking we could add two 'LS138s and control them both from one parallel port to do the scanning -- one for the display anodes and one for one side of the keyboard matrix."

"Oh. That could work." I nodded absently. "Can we get a look at the problem before we go looking for solutions?"

"Then it is a bug." Winston half-asserted, half-asked.

"Oh, yeah. I was planning on looking at it first or second thing today when everyone comes. Can you explain it to everyone, Winston?"

"Why me?" he complained, half-joking, half-resigned, as he picked up his schematics and went to the chalkboard. He started by putting up the working diagram from the day before while we waited for more of the group to come.

When he was done with that, and most of our group had arrived, he gave me a nod, and I nodded to Doctor Brown, and Doctor Brown said, "Any time you're ready."

Winston went back to his lab table and picked up his keyboard and controller, and announced, "Ground control, we have a problem."

Several of us chuckled as Winston proceeded to demonstrate how holding keys down would change a number shown on the display to some unintended shape.

"That's cool!" Carlos enthused.

"Solution one," Doctor Brown intoned with raised eyebrows. "It's not a bug, it's a feature."

Everyone laughed except Winston, who rolled his eyes before grinning lopsidedly. "That is one way of looking at it," he grudgingly admitted.

"Yeah," Doctor Brown grinned, "but I guess let's not use that solution this time."

"So, why does this happen?" I asked. "Show us what your circuit looks like."

Winston erased and redrew part of the circuit:


"So, we could ask Winston why he added another buffer," I pointed out, semi-rhetorically. "Or we could ask everyone if we all understand why. Suzanne, can you tell us why?"

She hesitated, than suggested, "If we don't have that, how does the computer know what key the controller decoded?"

"Exactly. Thanks. So, Winston, now what's the problem?"

"We don't have enough bits of I/O, and I have used one of the buffers for the displays as one of the buffers for the keyboard matrix." He erased and redrew another part of the diagram to show what he had.


"I was hoping that the scan could be done quickly enough to not interfere, but just holding the keys down messes things up."

"Yeah. So, Bob, you had a suggestion, right?"

"Jennifer thinks we should be able to do it without any additional LSI chips, but I think we need two 74LS138s."

"Can you diagram that?"

"Well, first, I think we need another bit for the main CPU interface."

"Why's that?"

Jennifer replied, "Main CPU has to be able to tell the controller what it's supposed to do, so it needs at least one bit of control."

Bob and Jennifer worked together to quickly put the following diagram up:


"Hmm. Let's keep that in mind. So, what about the 'LS138 one-of-eight demultiplexors?"

Bob modified parts of the diagram, to add the two demultiplexors:


"This would allow both the keyboard and the display to be addressed at the same time," Bob explained. "But if I shut down the displays for a hundred microseconds or so for the keyboard scan, I can share the select lines for the 'LS138s, and save a couple of port bits."

"Great. Thanks. Now I think Jennifer is right that, with a bit of really careful timing, and a bit of care in how we set up the ports for the matrix and displays, we could reduce the interference so that you could hardly tell that holding a key down is causing stray LEDs to light. But it would be kind of sensitive to pull-up or pull-down resistor values, and cheap parts might not work. And we might need to add diodes, as well."

Jennifer looked a little put-out at my conjecture.

"I'm not saying she shouldn't do it, just that that there are trade-offs, and I'm not prepared to dig into that right now. Back to the 'LS138s, how will they affect our code? Bob and Jennifer, let someone else answer."

Bob and Jennifer both nodded in agreement.

Javier finally spoke up. "Instead of shifting the matrix strobe bit and the display select bit on their ports, we count them?"

"Good. What does the counting look like?"

Everyone thought for a bit, and then Javier answered: "Counting for one of those can be just an INC instruction, but the other is going to be adding sixteen or something, depending on the bits used, right?"

"Exactly. So we'll look at that, but anyone have another way to do this? Jeff? Mark?"

"We're kind of with Mike on this," Jeff replied.

I turned to Mike, and he responded, "I'm planning on sharing a port between the keyboard and the displays, but I'll use a single eight-bit buffer to disable the keyboard when I'm using the displays. There are only seven digits, so only seven anode selects, and I can use the extra bit from the display select port to shut off the keyboard matrix except when I'm reading it."

"How about the control line for the main CPU to read the keyboard?"

"The Micro Chroma 68 keyboard interface only uses seven bits of data, so the high bit can be the key value strobe, like the Micro Chroma 68  documentation shows. I think that'll be enough." Mike stood up at a blank panel and sketched out his idea.


"Very good. Now we have at least a couple of different approaches that might work."

Larry asked, "Would it work to use two 6805s?"

"What do you think?"

"I guess we'd need a way for the main CPU to talk to the second controller."

"Sure. And that may have some advantages, as well."

Kyle laughed. "So we wouldn't need to bother with the timer and interrupts."

"We got 'em," Tim replied. "Might as well use 'em."

 Terry asked, "How about using a serial port for talking to the main CPU?"

"The Micro Chroma 68 design is looking for the keyboard on a parallel port," I pointed out. "But if we modify the design and the monitor ROM, we should be able to get a serial interface to work, as well."

Chuck objected, "The 6805 doesn't have a serial port."

"True."

After a moment of thought, he said, "But I guess a single bit of a parallel port could work, the way it's done on the Color Computer."

"Bit-banging. We could put the 6805's bit operators to good use there, I think."

Chuck nodded.

Carlos asked, "Which are you going to use?"

"I'll ask Julia later."

Several of the group chuckled.

"Because it's her keyboard and computer, not mine."

I turned and grinned at Julia, and she creased her brow and gave me a quizzical look.

Lupe raised a question -- "Does the Micro Chroma 68 use this status display we're adding?"

"Not with the current software, but, again, we can modify the software to use it, if we want to."

"Then maybe we don't really want the display on the keyboard controllers we're building now?"

"That is definitely a design option," I concurred. "Or, if you want the display but want to do it another way, you could add a 6821 to the mainboard and have the mainboard CPU control it directly."

Tanya complained, "So, basically, for the last three days, we've been on a wild goose chase."

"Make that exploring options," I reinterpreted. "And learning how to use a timer interrupt, so we can use timed samples instead of riding the port to debounce the keyboard matrix. And looking deeper into what we might want to build."

Larry said, "Okay, so can we get back to debouncing the keyboard now? I'm kind of interested in that. Can we use the same timer setup that we're using for the display to sample the keyboard matrix and debounce it?"

"Explain."

"The whole point of the debounce is to be sure we read one keypress as one keypress? We could remember which key we saw in a variable, and check it on the next timer interrupt?"

"How long do we wait to accept a keypress?"

Julia dug into her notes and recited, "300 words per minute is five words per second. At six letters per word, that's thirty keys pressed a second, or one thirtieth of a second."

I reached over and squeezed her hand, and she gave me a lopsided smile and a sigh.

Terry added, "That's about thirty-three milliseconds, so it's about thirty-three interrupts, the way we've set up the timer for the displays."

Freddie asked, "Can a key bounce take more than a millisecond to settle?"

Suzanne suggested, "We can count interrupts to time a keypress. Maybe sixteen interrupts is good to call it debounced."

I added, "Counting interrupts is good, and we can experiment with the duration, and we might even want to add keyboard repeat functions based on counting timer interrupts."

"Don't make things too complicated," Doctor Brown warned me.

"Hmm. Maybe we should try to describe the steps the interrupt handler routine is going to go through before we talk about keyboard repeat and rollover."

"Flowchart?" Tanya asked.

"How about you draw it?"

"I don't know what to draw."

"We can figure that out as a class."

Pat suggested, "How about pseudo-code?"

"Okay, you write pseudo-code while Tanya draws the flow-chart."

George laughed. "If you keep making people who suggest things work, everybody'll quit suggesting things."

I tilted my head. "I think we're all here to work, and taking notes is an opportunity for work, like any other."

I don't know if that was a very good sales line, but Tanya and Pat both stood up and cleaned up panes of the chalkboard to work on.

"What's the first thing our interrupt response routine does?"

"Clear the interrupt mask?" Larry suggested.

"What happens if the next interrupt comes before we're done?"

Everyone thought for a bit.

"Should I try to answer that?" Bob asked.

"Sure, go ahead."

"We lose track of part of the start of what the first interrupt was trying to work on."

"That's the underlying problem. What does the user see happening?"

"Uhm, I think there would be missed keys. And the display might lose digits."

"I think you're on target."

"So when should we enable interrupts?" Suzanne asked. "If we keep them disabled too long, that will also cause missed interrupts and missed keys."

Mike responded, "Missed interrupts, but keys can still be caught and display digits are less likely to be dropped, I think. So, enabling should be just before we return, I guess."

I agreed.

"And," he continued, we need to keep the interrupt routine as short as possible."

"Exactly."

"If that's the case, I guess we probably don't want to be translating the display digits to segment images at interrupt time."

"Okay."

"What did Mike just say?" Julia asked.

"Thank you again, Julia," Tanya applauded.

Bob replied, "SEGTBL has the segment images. I think Mike is suggesting that, instead of keeping a buffer full of the numeric values to display, we should keep a buffer full of the corresponding element of SEGTBL."

"Yeah," Mike confirmed. Although, I think it's just one or two instructions saved."

I added, "I think it also makes the display more flexible, but we'll talk about that later. Right now, it's the principle of the thing. Do we want to take time in the interrupt response to decode the keyboard?"

Pat hesitantly raised her hand, and Mike said, "Just ask, Pat."

"No? But how do we remember what key we're trying to debounce?"

I looked at Doctor Brown, and he shrugged.

I hesitated. "Well,  maybe we don't exactly want to remember which key."

*~*~*~*~*

Again, just how prescient should I imply, or claim, that I might have been?

Sure, I was prescient. I understood far more than the limited exposure I had in high school and during my time at Radio Shack might explain. I'm sure I brought a lot of that with me from the pre-existence -- from the world before birth -- into this world. But would I really have been able to work this one out in this world's technology without more experience?

Experience wins over theory any time you want to make something that actually works, and what I had in the previous world would have been more theory and principle, not so much application and experience, and definitely not experience colored and transmorgrified by the real-world reductions into -- implementations of -- real technology that were invented in this world since the time I was born.

Even had I been one of the in-crowd there which prepped the individuals who would be leaders of the industry in our present world, things changed drastically in the implementation. If you've ever worked on a systems development team, you know they always do.

And I suspect I was a rebel there, too.

I always had trouble in school with the question of how much I wanted to sully my understanding of principle with the details of real-world implementations. I knew my hesitance was worrying too much about what I would ultimately find relevant, but without experience who knows what is irrelevant?

Our best theories in this world fall so far short, even of what we knew then, not to mention falling so completely short of what God knows, understands, and does.

It doesn't matter. For the purposes of this novel, the me of this story must have been able to lead this discussion, or he could not have done what he had to do. But he has good company. There are other superhumans in this group.

*~*~*~*~*

Sheryl voiced the doubts most of the group had -- "How do we debounce a keypress we don't know we've seen?"

"Good question." I turned it back to her. "What is that microprocessor going to see when it scans the keyboard matrix?"

Sheryl thought and then replied. "A whole bunch of zeroes and one one."

"One one? Sounds like a dog barking."

Everyone looked at me questioningly.

"Japanese joke. Woof-woof is wan-wan in Japanese. Sorry. Never mind."

Sheryl wasn't the only one shaking her head, but she replied. "Just one, I think."

"Does everyone agree?"

We had some nods and some blank looks.

"Okay, let's look at some possible code for this. We'll assume Mike's buffer for separation, with the keyboard matrix enabled. SHARED is the port shared for strobing the keyboard and setting the display segments, and KBDIN is the other edge of the matrix." I pointed to the buffers as I mentioned them. "Scanning will begin something like this," I erased a panel and wrote
    SEC
STBLUP
    ROL SHARED
    LDA KBDIN
"What happens next?"

"Mike?" Winston probed.

"I know what I'm doing. We all need to think."

Larry said, "Well, the natural response is to wait for a one, but we have to look on seven other rows, too. And Joe keeps harping about busy-waiting, so I don't think that's what he thinks we should do."

"You can busy-wait if you choose. I'm just trying to point out how to sample instead."

"Sample?" Terry piped up. "If we're going to sample, we need to record all the samples."

I held up the chalk, and she stood and took it, then went to the chalkboard and added to what I had written:
KBDST RMB 8
*
    LDX  KBDST ; STATE ARRAY
    CLR SHARED
    SEC ; READY TO STROBE
STBLUP
    ROL SHARED ; STROBE
    BCS KBDDUN ; DONE YET?
    LDA KBDIN ; COLUMN STATE
    STA ,X ; SAVE IT
    INX ; NEXT
    BRA STBLUP
KBDDUN
"That records the entire keyboard state. First I point X to the keyboard state array in page zero, I mean, direct page, RAM. Then I clear the shared port so we can shift the strobe across it. Then I set the carry flag to be shifted in.

"Rotate left shifts the carry flag in the SHARED port for the strobe. First time, it shifts the carry flag in. Branch on carry set will stop us when the carry flag is rotated out of the high bit."

"Note," I pointed out, "that branch on zero, BEQ, will also work and may be more resistant to hardware failures."

Julia raised her hand. "What? Is that supposed to go in the notes?"

"In case of short circuits or other bad things that could happen to the keyboard," I explained.

Wallace was writing the notes. "Got it."

Julia gave me a perplexed look. "I guess we can go on, but explain that tonight, please."

"Well, if the strobe bit is shifted out, the carry will be set, but so will the zero flag, because there will be no ones left in the port output register. It's a different way to test for the end of the loop, and we might want to take the time to consider which will be less likely to have problems when there are hardware failures."

"Does it matter on a keyboard?" asked Javier.

"Maybe, maybe not."

Doctor Brown cleared his throat. "Leave it in the notes, and add that we really don't have the tools for the analysis yet."

I exchanged glances with him.

"Right," I concurred. "Something to think about later."

"I can continue?" Terry asked.

"Please."

"Then we load the column state from the KBDIN port and store it where X points into the state table, and bump X before branching back. I don't think I need to clear the carry bit after incrementing X because there's nothing in the loop that would set it besides the rotate."

"Good, --"

Wallace interrupted. "But why check them all? Why not jump out of the loop on the first scan that's not all zeroes?"

Terry pointed out, "There may be other ones, as well. You don't always release one key before pressing another."

"I guess that's true." Winston tilted his head. "But can we handle that?"

"I think we can. Terry, can you walk us through how that would work?"

"What we want to see is which bits changed." She thought for a moment. "So we need to keep a record of the previous sample, too."

"I think so. It uses a lot of the MCU's little bit of RAM, but I think we need to keep three records."

"Do we have time to copy all of it?"

"Maybe. I think we can do it without copying three times, but let's look at copying first. I think it would be easier to understand."

Terry nodded, erased her code and started over.
KBDST0 RMB 8 ; NEWEST
KBDST1 RMB 8
KBDST2 RMB 8 ; OLDEST
*
    CLX ; FIRST COLUMN
    LDA #1
    STA SHARED ; STROBE
STBLUP
    LDA KBDST1,X ; COPY 2ND
    STA KBDST2,X ; TO OLDEST
    LDA KBDST0,X ; COPY PREVIOUS
    STA KBDST1,X ; TO 2ND
    LDA KBDIN ; GET STATE
    STA KBDST0,X ; SAVE CURRENT
    INX ; NEXT COLUMN
    ROL SHARED ; NEXT STROBE
    BCC STBLUP ; UNTIL CARRY
"That's pretty tight programming."

"Thank you."

"After finishing the copying, is there anything left to do before returning from the interrupt handler routine?"

"Wait," Julia said. "Pat and Tanya are stuck."

I laughed. "Sorry. I'm getting carried away. This little routine should be called 'Copy Keyboard State' or something." I paused until they were ready.

"It starts with zeroing your index and prepare the strobe for the first column. Then you enter a loop with no tests at the entry point." Again, I paused.

"Copy the previous state over the oldest state for this column.

"Copy the most recent state over the previous state for this column.

"Read the new current state and save it."

"Bump index and shift strobe for next column.

"Finally, if the strobe bit has shifted out, we're done."

Pat's pseudo code looked like this:
Copy Keyboard State:
    Zero column index;
    Prepare column strobe;
    Loop --
         Overwrite oldest state for this column with 2nd oldest;
         Overwrite 2nd oldest state with most recent;
         Overwrite most recent state with current;
         Bump column index;
         Shift strobe bit left;
    Until no columns left to strobe.
Tanya's flow chart looked like this:


"So, Terry, is there anything left to do?"

"If we've already done the display stuff when we get here, I don't think of anything else."

"Very nicely done. And that was no idle compliment. Let's count the cycles to see if that can all happen withing a millisecond."

Everybody got involved in digging into the cycle counts from the specsheets and adding them up.

"CLX is four cycles."

"Three cycles for CMOS."

"These are CMOS chips aren't they?"

"Yeah."

"I'm counting HMOS cycles, just to be safe."

And so forth, some using the detailed timing charts and others using the summary in the op-code table.

"Fifty cycles through one loop."

"Roughly, worst case."

"Times eight is four hundred cycles worst case."

"That's not quite half the time between interrupts."

"Is that too much?"

"It'll only happen once every sixteen milliseconds, or so, right?"

"We'll have to test it."

"Joe said we could avoid all the copying."

"Well, we can avoid copying the previous and second previous. It's a little less straightforward, because we have to keep track of which one is the oldest and only overwrite that one. Then remember which is the new oldest. I think that should cut the time down under a hundred cycles. But I think four hundred cycles, plus the time for the interrupt counting, etc., isn't too much. Some of you might want to try both. Or you could also adjust the timer interrupt, to two or four milliseconds."

That gave everyone something to work with for a bit, fitting the processes to their own circuits.

Then Carlos asked, "Okay, we've recorded all this state, now what? How do we check for someone hitting a key?"

"Good question." I checked the time before asking, "Any suggestions?"

Carlos frowned. "Looking for ones is not the answer, I guess."

"Almost."

"Look for changes."

"Right."

"Compare instruction, CMP?"

"That'll only tell us if there's a change. It won't tell us which changed. There's a way that can tell us which of eight bits changed with just one instruction," I prompted.

"The exclusive-or instruction?"

"Bingo."

"Does that mean we have to use eight more bytes to remember which bits went high?"

"Yes -- or to remember which changed and are no longer bouncing. We can check the most current state to figure out whether the change was a press or release."

"And this is not in the interrupt routine?"

"Right."

"How do we know when to look?"

"How about checking the interrupt count?"

Carlos shrugged. "Uhm, explain that?"

"After the interrupt routine counts or so and records the new samples, it can leave the count at sixteen, and the non-interrupt code can see that sixteen, check and record which changed, and subtract the sixteen from the count to start over. Or clear the count if we are sure we don't get interrupted. But subtracting would be safer."

"Hmm. I guess I need to think about that."

I checked the clock again. "Well, I need to go deliver my newspapers. Who wants to lead the discussion of how to turn this into code, and how to work out the key codes from the bits that changed, and how to buffer key codes that the main CPU hasn't read yet? Mike?"

"I vote for letting Jennifer lead it."

"Why not? I'll let you guys work it out. I have to run."

*****

When I got back, they had worked out recording the changed bits, and were working out out to turn a changed bit into a bit number, to figure out which key was pressed. I suggested not loading the bit or count into the index or accumulator, to avoid having to save and restore them:
KBIT RMB 1 ; BIT TO COUNT
KBITCT RMB 1 ; BIT # COUNTER
*
    CLR KBITCT ; START AT BIT 0
KBITLP
    SHR KBIT ; SHIFT DOWN
    BEQ KBITDN ; DONE IF OUT
    INC KBITCT ; COUNT BIT
    BRA KBITLP ; DO IT AGAIN
KBITDN
* BIT NUMBER NOW KNOWN
There were several using math to convert the keyboard matrix to ASCII codes, but some had keyboard matrices that varied enough from ASCII to make that hard. So I showed them how to put the key codes in an array to match the keyboard matrix they had, using the bit number concatenated with the column number times eight to index the array.

* COLUMN INDEX IN X
    SHLX ; TIMES 8
    SHLX
    SHLX
    TXA ; COLUMN NUMBER TO A
    ORA KBIT ; KBIT <= 7

    TAX ; MATRIX ELEM #
* USE THIS TO INDEX
* ASCII CONVERSION TABLE
George complained, "I've heard that global variables are bad, but we're using globals all over the place."

Jennifer complained, as well. "Doctor Brown just laughs at us when we worry about it."

"I'm only here to make sure you don't destroy the lab." If anything, his grin was even broader. "And steal ideas for my lesson plans next year."

That provoked much laughter, and a release of stress.

"Seriously, though. I'm getting a lot of good ideas from watching you guys. And I want to see what Joe has to say before I weigh in."

"I'm not sure what I have to say, but it seems to me there are several problems in organizing our variables. One is where you declare them in the source program. We've been declaring them as we invent code snippets, then moving the actual declarations into the direct page variable list when we put the snippets into our code.

Jennifer tilted her head. "What about variables on the stack? Doesn't the 6805 have a way to put variables on the stack?"
"First question, which stack?"

"Now it gets interesting," Doctor Brown intoned.

"Which stack?" Winston echoed. "Is there more than one?"

"The 6805 doesn't have a parameter stack, perhaps because Motorola figures, if the code is complex enough for re-entrant parameters, it's complex enough to use something more powerful."

"Then why," asked Jennifer, "the jump subroutine instruction?"

"Cheap on transistor count and allows some simple code reuse? I guess. Anyway, that appears to be the approach we should take, graduating to a more powerful processor when we want to put parameters on a stack."

Julia shook her head. "I don't think I'm the only one who's lost."

"Can we talk about this tomorrow? I'm kind of hoping we can get some of the keyboards working today."

Doctor Brown laughed. "Classic maneuver, but I agree. Too early for a lot of you to talk about re-entrance and stacks."

Having only one computer to program 68705s on was a bottleneck, but we were able to get Winston's, Mark's, Jeff's, and Suzanne's keyboards working, and they plugged them into their Micro Chroma 68+6803s and started playing with the TV-BUG monitor. Some of the students who were waiting watched the fun.

Julia declined to use group time to program her keyboard. "I didn't bring the Micro Chroma today, anyway."

"Oh, is that why tomorrow?" I asked, and she just gave me a kind of thoughtful look.

"Sure. That's why." And she winked at me, but wouldn't explain further.

"Okay."

We got a few more keyboards running before time to shut down.

*****

"Intel 8086?" Julia looked at the book I was borrowing from the library. "What do you need that for?"

"Ammunition."

*****

After studying and singing, Julia programmed her keyboard, then plugged it into my Micro Chroma 68 to test and play with it while I studied the i8086/8088 manual and the TMS 9900 and 9940 manuals.

"Remember that BLWP instruction I was looking for last night?"

"Yeah."

"You can use it to allocate variables on a stack. When you leave a subroutine, you deallocate the variables."

"Allocate, I think I can understand. That's like reserving space for working on things?"

"Right. Reserving space to work on things."

"So deallocate would be giving the space up?"

"Right again."

"I guess that makes better use of RAM."

"It also means that, if you have more than one process running, the variables on the stack are local -- kind of private -- to the the process that owns the stack.

"I'll have to think about that. So what kind of ammunition are you getting from the 8086 book?"

"Registers that have limited uses. It's better than the 8080, but they are still pretty limited, compared to the 6800's use of memory."

"Do you think they have a reason for that?"

"Simplifying their circuitry, mostly, I'm sure. Add special purpose features in the transistors that become extra."

"Marketing?"

"Yeah."

I read in silence for a bit more. "Really weird address registers. It's like they deliberately put holes in addresses."

"Huh?"

I showed her a diagram of the 8086 segment registers and how addresses are assembled in the original 8086 segmentation.

"I can't even begin to understand what they are trying to do."

"The actual address registers are sixteen bit. So are the segment registers. But the segment registers are slid over four bits, before being added, so you can play address games and use two megabytes of RAM, if you do it right."

"So you can add more RAM. Do Motorola processors do that?"

"There is a memory management unit for the 6809, and, I think, the 6800, that allows a megabyte of memory to be addressed, and allows memory to be moved around in two kilobyte pieces to mix and match. The 8086 gives a cheaper, half-baked approach that doesn't really allow mixing and matching, except in sixty-four kilobyte sliding frames."

"Hmm."

"The 68000 allows completely linear segmentation, if you want to use it. No shifting things around."

"I'll have to figure out what this is all about later."

"Ah. The 8086 has instructions to support stack frames, kind of like the 9900. And it becomes clear to me why I don't like the idea of keeping return addresses in the stack frame."

"Return addresses?"

"So you can get back to the code that called a subroutine, you save the program counter on the stack. With parameters and locals on the same stack, you're going to have return addresses and parameters and locals colliding. And bad things will happen, like returning to data instead of code, and then who knows what happens next."

"Who knows? I'm getting sleepy."

"Like when my Micro Chroma's reset switch bounced and it would cause random text and flashing meaningless graphics to show on the screen."

"Oh."

"Let's get you home."

We shared a quick kiss on her doorstep, and her mother opened the door and cleared her throat.

"Mom, not tonight. I've been studying stuff that is completely foreign to me. Joe calls it playing, but I'm tired."

"I have to admit to being a bit tired myself," I added. "Julia's keyboard will now talk to her computer. Tomorrow's going to be another long day, I think."

"Speaking of her computer," Mr. Cisneros spoke up from the couch, "I think she needs a box to put it in."

"True."

I went in and sat down, and we discussed plans for an enclosure for her computer for maybe a quarter of an hour, Julia and I holding hands in between making sketches and using our hands to talk about the shapes of things. We talked about a box like mine, and we also talked about putting her keyboard in a separate enclosure so she could move it around separately from the computer.

Julia put the sketches with her notes, and we shared another kiss before I left.

Chapter 13.5: what?

[Backed up at https://joel-rees-economics.blogspot.com/2020/07/bk-33209-straits-keyboard-decoding.html.]